tags:

views:

137

answers:

2

MSDN defines System.Enum as an abstract class:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible

And also MSDN make such statement about abstract class:

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

But I simply cannot derive from System.Enum. According to Jeffery Richter in his book "CLR via C#", it is C# compiler that forbid that derivation.

And I checked System.Enum's members, most of them are static, which is reasonable for it cannot be instantiated and static methods don't need an object instance to invoke. But there are also some instance methods, such a GetHashCode() and GetTypeCode().

So, my first question is, if System.Enum cannot be instantiated or derived, how could these instance methods be invoked? Isn't this some kind of a paradox?

I know I could use the following approach to invoke these instance methods, but why? Is there some kind of System.Enum or derived type object instance created? When? and by whom?

public enum Days:byte { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
Days d = Days.Friday;
d.GetTypeCode();
d.GetHashCode();
+2  A: 

Enums are value type that why you can't instanctiate or derive from it. Instance methods such as GetHashCode() are just present for reference type objects such as classes.

Jojo Sardez
+4  A: 

You can derive from Enum, e.g.

public enum MyEnum
{
    // ...
}

Will be compiled down to something like:

.class public auto ansi sealed MyEnum
    extends System.Enum
{
    // ...
}

The restriction the C# compiler places is that it won't let you write the derivation manually, but instead requires you to use the enum keyword when declaring the type.

As to why this is... my guess is because enumerations are a bit weird in the CLR. For example, the ValueType and Enum base types are both themselves reference types, not value types. There's a whole bunch of magic going on under the covers, and if you force the use of a keyword and prevent derivation from the magic base class, then you are keeping your intent separate from the magic.

Greg Beech
Thanks, Greg. Your answer partially solved my problem. Now I am wondering why not allow us just extends the System.Enum directly?
smwikipedia
@smwikipedia - Just added my guess as to why this is. You'd have to get one of the C# language designers to verify it though (Eric Lippert is on here regularly so he might stop by with any luck).
Greg Beech
@Greg, thanks, I have sent a mail to Eric Lippert and hope he could reply soon.
smwikipedia
Couldn't have said it better myself. :-) Note that we also do not allow you to derive from System.ValueType or System.Delegate directly either. Types like System.Enum are *very special types*. We can't let people say ridiculous things like "class List<T> : System.Delegate" and expect it to work.
Eric Lippert
Thanks, Eric. I feel much better now. 8^D
smwikipedia