473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Structure C# internals

I know structure is a value type and derived from System.ValuType. So I have
the following questions,.

1 - C# won't allow to derive one structure from other. Then how come it is
derived from System.ValueType ?

2 - I have written a simple C# console application. Following is the
equivalent IL code which is generated for the structure

..class sequential ansi sealed nested private beforefieldinit MyStruct
extends [mscorlib]System.ValueType
{
}

In this it is showing as class. How this is possible ?

Any help would be useful

Thanks
Jun 27 '08 #1
7 1510
On May 8, 11:41 am, Navaneeth.K.N
<Navaneet...@discussions.microsoft.comwrote:
I know structure is a value type and derived from System.ValuType. So I have
the following questions,.

1 - C# won't allow to derive one structure from other. Then how come it is
derived from System.ValueType ?
The details of how value types fit into the type hierarchy are
somewhat murky and involve being *very* precise with terminology. Here
there's no particular conflict, however: System.ValueType isn't a
value type itself, it's a reference type - so the "you can't derive
from value types" rule remains intact.
2 - I have written a simple C# console application. Following is the
equivalent IL code which is generated for the structure

.class sequential ansi sealed nested private beforefieldinit MyStruct
extends [mscorlib]System.ValueType
{
}

In this it is showing as class. How this is possible ?
Because ".class" doesn't mean what you think it does (and what most
people would naturally think it does).
".class" is used to declare various kinds of types:

From ECMA 335:

<quote>
[Rationale: For historical reasons, many of the syntactic categories
used for defining types incorrectly use
“class” instead of “type” in their name. All classes are types, but
“types” is a broader term encompassing value
types, and interfaces as well. end rationale]
</quote>

Jon
Jun 27 '08 #2
Navaneeth.K.N wrote:
I know structure is a value type and derived from System.ValuType. So I have
the following questions,.

1 - C# won't allow to derive one structure from other. Then how come it is
derived from System.ValueType ?
System.ValueType, despite its name, is a reference type. It's basically
a join type to attach value types into the reference type hierarchy.
It's treated magically by the CLR, in that descendants of it are value
types. Similarly, EnumType isn't an enumeration, etc.
2 - I have written a simple C# console application. Following is the
equivalent IL code which is generated for the structure

.class sequential ansi sealed nested private beforefieldinit MyStruct
extends [mscorlib]System.ValueType
{
}
The '.class' construct in ILASM would be more accurately descriptive if
it was spelled '.type'. It introduces a new type, not necessarily a
reference type.

-- Barry

--
http://barrkel.blogspot.com/
Jun 27 '08 #3
Thanks John, that was an excellent post.

System.ValueType isn't a
value type itself, it's a reference type - so the "you can't derive
from value types" rule remains intact.
This is making bit confusion. As you know value types are allocated to stack
(there are exceptional case). So being a reference type, how descendants of
System.ValueType is getting allocated in stack ? It should go to heap, right ?

Is it possible to create a custom type which is derived from
System.ValueType ?

Thanks again for the reply

Navaneeth
Jun 27 '08 #4
Hi Barry,

Thanks for the reply. It was helpful.

Navaneeth
Jun 27 '08 #5
On May 8, 2:12 pm, Navaneeth.K.N
<Navaneet...@discussions.microsoft.comwrote:
System.ValueType isn't a value type itself, it's a reference type - so the "you can't derive
from value types" rule remains intact.

This is making bit confusion. As you know value types are allocated to stack
(there are exceptional case).
Well, they're allocated "inline" with whatever their context is:
http://pobox.com/~skeet/
So being a reference type, how descendants of
System.ValueType is getting allocated in stack ? It should go to heap, right ?
Basically the CLR knows that types derived from ValueType are value
types, and does the right thing.
Is it possible to create a custom type which is derived from
System.ValueType ?
Sure, write a struct in C# :)

I don't believe you can create your own reference type deriving from
ValueType.

Jon
Jun 27 '08 #6
Hi

Thanks again for the reply. I will be glad if you can look into the
following point.

1 - As per this
(http://msdn.microsoft.com/hi-in/libr...us,VS.80).aspx) MSDN
article, .NET having two types of value-types. Built in and user-defined.
They say user defined types are derived from "System.ValueType". But what
about int32, boolean etc ? Is that also derived from "System.ValueType" ?

2 - IL allows deriving from System.ValueType. But why C# is restricting us
to derive from that ? any ideas ?

Thanks again

Navaneeth
Jun 27 '08 #7
On May 9, 6:25 am, Navaneeth.K.N
<Navaneet...@discussions.microsoft.comwrote:
Thanks again for the reply. I will be glad if you can look into the
following point.

1 - As per this
(http://msdn.microsoft.com/hi-in/libr...us,VS.80).aspx) MSDN
article, .NET having two types of value-types. Built in and user-defined.
They say user defined types are derived from "System.ValueType". But what
about int32, boolean etc ? Is that also derived from "System.ValueType" ?
Yes. Take a look at the docs for System.Int32 etc.
2 - IL allows deriving from System.ValueType. But why C# is restricting us
to derive from that ? any ideas ?
IL doesn't let you derive a reference type from System.ValueType, only
a value type - and C# lets you do that too, by declaring a struct
instead of a class.

Jon
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ma740988 | last post by:
I sat through - what should have been a 10 minute discussion where at issue is the 'security of a class'. Truth is I was puzzled by the soruce, so much so that I lost track of the end result. In...
7
by: ej | last post by:
I'm trying to figure out how to get at some of the internal interpreter state for exception handlers and debug statements in general. I just read through Section 8 of the Python Tutorial. I see how...
4
by: Niklaus | last post by:
Hi, I would like to know more about casts.What exactly happens when casts are applied to a variable.I know that if an object of type int is applied an cast of float the result would be of type...
17
by: ypjofficial | last post by:
Hello All, I have read in many c++ literature that vtable is nothing but an array of pointer to virtual functions inside a class.And the class where the virtual function/s are declared stores the...
18
by: jparus | last post by:
Hello, can anybody explain me how to port FILE structure into other languages. I have an DLL library and functions in this library take FILE* as a parameter. I want to make an interface into...
4
by: gordon | last post by:
Hi I am still fairly new to C#.net and I sometimes make basic program design mistakes - particularyly in the context of paying attention to OOP principles. At the moment I am working on an...
1
by: Jobs Gooogle | last post by:
Skills: .Net VC++ Java C++ Windows Internals Unix Internals Location: Bangalore, Hyderabad, Delhi (NCR), Chennai Experience: 3+ Yrs Hand-On Please forward your profiles to mailto:...
18
by: Guru Jois | last post by:
Hai, Can I get some docs or links to learn the C compiler internals from basic to advanced. It must contains good documentation of how compilers allocates memory to variables. Bye Guru Jois
1
by: Robert | last post by:
If the goal is to take a 1-dimensional package of text and produce a tree data structure that is easy to understand and manipulate, and then write C/C++ code that works with that tree - does...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.