Re: Type alias in C#



It also won't work with value types...

Hence "struct" in that list.

Not all value types are structures, for example, enum.

All value-types are structs. Enum is a struct as well.

I'm not so sure. Lutz Roeder's refector declares Enum as a 'public
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.



.



Relevant Pages

  • Re: Type alias in C#
    ... object that derives from ValueType can be considered a structure. ... Not all value types are structures, for example, enum. ... Lutz Roeder's refector declares Enum as a 'public ... Structs are not really a CLR notion -- value types are. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: mutually referential (Pg 140 K&R2)
    ... >> struct s *p declares p using an incomplete struct s type, ... You can't forward declare an enum ...
    (comp.lang.c)
  • Re: Is enum type derived from Enum struct?
    ... >struct isn't be inherited. ... Enum is actually a class, unlike everything else that derives from ... ValueType. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Type alias in C#
    ... Not all value types are structures, for example, enum. ... All value-types are structs. ... Enum is a struct as well. ... Anything that derives from System.ValueType is a value-type. ...
    (microsoft.public.dotnet.languages.csharp)
  • [PATCH][v4][1/24] Add core InfiniBand support (public headers)
    ... +struct ib_fmr_pool_param { ... +enum ib_device_cap_flags { ... specifies the QP attributes to modify. ... The protection domain associated with the memory region. ...
    (Linux-Kernel)

Loading