4

Possible Duplicate:
C# int, Int32 and enum's

C# allows you to set the underlying type of an enumeration to long. But, how would you explain the difference in behavior when you try to compile the following two statements:

public enum Colors : long
{
   Blue = 512L,
   Purple = 1024L
}

and

public enum Colors : System.Int64
{
   Blue = 512L,
   Purple = 1024L
}

The first one compiles oK (with : long), while the second (with : System.Int64) wont compile - you get an error: Type byte, sbyte, short, ushort, int, uint, long, or ulong expected Note: Obviously, I understand the error message. What has me baffled is that I thought that "long" is more or less an alias for "Int64"

Community
  • 1
  • 1
John Gathogo
  • 4,495
  • 3
  • 32
  • 48
  • hmm... @BoltClock. your link was useful. It has a healthy discussion about the issue. It would however be hard to deduce that it addresses the same subject matter by just looking at its title. Anyhow, thanks. – John Gathogo Sep 22 '11 at 07:39

1 Answers1

3

It is a limitation of the C# compiler and support for this on Enum will not be introduced. You will have to use long. Alex Turner (a Program Manager for Visual Basic and C#) at MSFT:

Posted by Microsoft on 6/25/2010 at 8:53 AM Thanks for the suggestion for Visual Studio!

As you point out, we could enable support for saying Int16 instead of int here, but this would not provide any extra expressiveness to C# programs (and it's more characters to type!). We'd be unlikely to invest our resources to add this support to Enums.

David Anderson
  • 13,558
  • 5
  • 50
  • 76
  • It is actually fixed by now, see https://stackoverflow.com/questions/37589056/why-is-this-enum-declaration-working-now – Mafii Aug 16 '17 at 14:24