Share via


What can enums derive from

I thought I knew the answer but a discussion in the internal alias proved me wrong :)

 enum e1 : int
{
}

enum e2 : Int32
{
}

The first enum complies fine but the second doesn't. The reason is apparent from the compiler error message "error CS1008: Type byte, sbyte, short, ushort, int, uint, long, or ulong expected". It means that the C# compiler accepts only built in types. The reason is simply because one can define a class as follows

 class Int32
{
}

In this case enum e2 : Int32 will be invalid. The compiler could've taken extra pains in ensuring that the .NET types are not overloaded but it'd have extra cost which the compiler guys didn't take.

Thanks to Cyrus for the info. It's unfortunate that he has stopped blogging he used to be among my favorite bloggers.

Comments

  • Anonymous
    April 20, 2007
    I wonder what happened to him? I guess part of me just assumed he was no longer with Microsoft.