Structure C# internals

  • Thread starter Thread starter Navaneeth.K.N
  • Start date Start date
N

Navaneeth.K.N

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
 
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
 
Navaneeth.K.N said:
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
 
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
 
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
 
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/library/34yytbws(en-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
 
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/library/34yytbws(en-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
 
Back
Top