Re: Type alias in C#
- From: Dustin Campbell <dustinc@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 7 Nov 2006 18:19:44 +0000 (UTC)
I'm not so sure. Lutz Roeder's refector declares Enum as a 'publicAll value-types are structs. Enum is a struct as well.Not all value types are structures, for example, enum.It also won't work with value types...Hence "struct" in that list.
abstract class' and the System.Type class has a specific 'IsEnum'
property, the only listed value type to have its own specific 'IsX'
property. It may not a be a 'class' (IsClass returns false) as such,
but I'm not convinced that it's a struct either.
Structs are not really a CLR notion -- value types are. Part of the confusion here is that the term "struct" and "value type" are being passed around as synonomous -- which they really aren't. All C# structs are value types but not all value types are C# structs.
At the CLR level, there are only classes. In fact, interfaces are actually classes too but have a specific bit set that denotes them as interfaces and causes them to be treated differently. A value type is simply a class that derives from System.ValueType. An enum is a class that derives from System.Enum (which derives from System.ValueType). The CLR simply treats classes that derive from System.ValueType in a special way -- passing them by value instead of by reference and allocating them on the stack when declared in a method body.
Lutz Roeder's reflector can be a bit misleading if you leave the language set to C#. Setting it to IL will show the reality of how types are really declared. Consider this declaration of System.Int32 in IL:
..class public sequential ansi serializable sealed beforefieldinit Int32
extends System.ValueType
implements System.IComparable, System.IFormattable, System.IConvertible, System.IComparable`1<int32>, System.IEquatable`1<int32>
It is really a class that derives from System.ValueType and implements a set of specific interfaces. There is nothing remarkably different about how System.Int32 is declared versus, say, System.StringComparer:
..class public abstract auto ansi serializable beforefieldinit StringComparer
extends object
implements System.Collections.IComparer, System.Collections.IEqualityComparer, System.Collections.Generic.IComparer`1<string>, System.Collections.Generic.IEqualityComparer`1<string>
The only difference is the base type that it derives from. Enums are no different -- consider System.StringComparison:
..class public auto ansi serializable sealed StringComparison
extends System.Enum
Enums *are* classes at the CLR level. Reflection attempts to make distinctions between different types to clarify them for the client. So, it is not a great tool to use to try and understand how things are really put together at the metadata-level.
System.Enum derives from System.ValueType.Which does not necessarily make it a structure.
You're correct, it makes it a value type. Clarification of terminology is definitely needed here.
Best Regards,
Dustin Campbell
Developer Express Inc.
.
- Follow-Ups:
- Re: Type alias in C#
- From: Dave Sexton
- Re: Type alias in C#
- From: Christopher Ireland
- Re: Type alias in C#
- References:
- Re: Type alias in C#
- From: Christopher Ireland
- Re: Type alias in C#
- Prev by Date: Re: Save a string to a text file
- Next by Date: Re: Save a string to a text file
- Previous by thread: Re: Type alias in C#
- Next by thread: Re: Type alias in C#
- Index(es):
Relevant Pages
|
Loading