Roslyn: The underlying type of an enum does not have to be a simple type

Created on 3 Jun 2016  路  7Comments  路  Source: dotnet/roslyn

Version Used: 1.0.0, 1.3.0-beta1-20160602-01

Steps to Reproduce:

Compile the following code:

c# enum E : System.Int32 {}

This compiles successfully, while the C# specification says it shouldn't.

For background, see this SO question. To sum up, the C# specification (version 5.0 or preliminary version 6.0) say that _enum-base_ has to be an _integral-type_, where _integral-type_ includes only simple types (i.e. int, but not Int32):

_enum-base_:
    : _integral-type_

_integral-type_:
    sbyte
    byte
    short
    ushort
    int
    uint
    long
    ulong
    char

Since I do not see any harm in allowing this and since disallowing it now would be a breaking change, I think the specification should be changed.

Area-External Area-Language Design Bug Documentation

Most helpful comment

This is one of the changes made in C# 6.

All 7 comments

Isn't System.Int32 just an alias to int?

@AdamSpeight2008 It's the other way around (int is an alias for System.Int32). But the grammar here says that you have to use int and that System.Int32 is not allowed, even though both are logically the same.

Also, the old native compiler does not allow this code:

error CS1008: Type byte, sbyte, short, ushort, int, uint, long, or ulong expected

Easier to alter the specification text to includeor is an alias to an integral type.

Easier to alter the specification text to includeor is an alias to an integral type.

I would say it that this is the best/most intuitive approach. It also seems more logical than the old spec text.

This is one of the changes made in C# 6.

Mads, assigning to you to ensure it ends up in the C# 6 specification. Your latest draft lacks this modification.

Was this page helpful?
0 / 5 - 0 ratings