35

If int is synonymous to Int32 why does

enum MyEnum : Int32
{
    Value = 1
}

...not compile? Where as

enum MyEnum : int
{
    Value = 1
}

will, even though hovering the cursor over the int word will display struct System.Int32?

Joey
  • 344,408
  • 85
  • 689
  • 683

2 Answers2

30

The underlying type is indeed the same, but the compiler depends on the type to be as the exact alias. This is a compilation error based on parsing. I took a look at C# grammar specification and the underlying types defined there as tokens based on the alias (e.g. 'int', 'unit'... etc.). The parser expects specific strings from the integral-types grammar rule.

The error is a parsing error even though both enum Enum : int means the same as enum Enum : Int32.

I don't know the reason for forcing this limit to parsing step, but I can try guessing: Since Int32 is not a keyword it might refer to something other the actual int struct. If the parser has to know the type in order to build different AST for each base type then it cannot depend on token which is not a keyword.

Even though the C# specification defines the int keyword as explicit alias System.Int32, it's still a problem to get this information about the explicit type (Int32) during parsing step since it requires a lot of context information which cannot be inferred at this step.

Elisha
  • 23,310
  • 6
  • 60
  • 75
  • 1
    See also this MS Connect bug where they explain the justification for not changing the bahvior: http://connect.microsoft.com/VisualStudio/feedback/details/557064/c-enum-declaration-only-accepts-value-type-alias-eg-short-int-long-instead-of-net-valuetype-eg-system-int16-system-int32-system-int64 – Michael Edenfield Mar 21 '12 at 22:56
  • @MichaelEdenfield that Microsoft Connect links is not accessible. Can you checkout it should be visible publicly? I get this error `The content that you requested cannot be found or you do not have permission to view it. If you believe you have reached this page in error, click the Help link at the top of the page to report the issue and include this ID in your e-mail: e4c85df6-9343-4045-88d2-fc2d64bd01de ` – Jeroen Wiert Pluimers Mar 06 '13 at 14:27
  • 1
    No, unfortunately they expire connect links after certain points. The gist of the bug was that "int" is a keyword and "Int32" is a type, and the parser currently expects a valid "keyword" as the enum base type. Changing the behavior requires changing the order of the keyword-to-type substitution vs. enum-type-parsing steps and is a big change for a tiny, tiny benefit, so it will likely not happen (unless there happen to be other related changes in the same area.) – Michael Edenfield Mar 06 '13 at 15:34
  • Thanks. Found a different one: it is marked as "Won't Fix": https://connect.microsoft.com/VisualStudio/feedback/details/737759/c-compiler-requires-enums-underlying-type-specified-by-its-type-alias-keyword-instead-of-type-name – Jeroen Wiert Pluimers Mar 07 '13 at 12:11
  • Works now, see https://stackoverflow.com/questions/37589056/why-is-this-enum-declaration-working-now – Mafii Aug 16 '17 at 14:25
15

A familiar curiosity... the language spec states (14.1):

An enum declaration may explicitly declare an underlying type of byte, sbyte, short, ushort, int, uint, long or ulong. Note that char cannot be used as an underlying type. An enum declaration that does not explicitly declare an underlying type has an underlying type of int.

But since int is generally just an alias for System.Int32 it isn't unreasonable to think either might work... but indeed it doesn't. It isn't generally a big problem, but intriguing none the less.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900