Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum members of Int32 type [duplicate]

Tags:

c#

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

It could be a rather basic / simpler question I am creating an enum in the following way.

Case 1 does compile perfectly. But Case 2 throws an error. To my understanding int and Int32 mean the same in C#.

Case 1

    [Flags]
    public enum MyEnum : int
    {
        Red = 0x1,
        Green = 0x2,
        Blue = 0x4
    }

Case 2

    [Flags]
    public enum MyEnum : Int32
    {
        Red = 0x1,
        Green = 0x2,
        Blue = 0x4
    }

What's the difference here and why C# doesn't compile the code when the enum's members are specified to be of Int32 type?

like image 278
SaravananArumugam Avatar asked Sep 23 '10 00:09

SaravananArumugam


People also ask

Can enum have duplicate values?

Enums can not have duplicate values.

Can two elements in enum have same value?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

Is enum same as int?

The enum constants are int s. They're just hopefully meaningful names for some integer constants.

Can enum have duplicate values C++?

There is currently no way to detect or prevent multiple identical enum values in an enum.


1 Answers

From a Microsoft bug report here:

Symptom
If you use System.Int16/Int32/Int64 instead of Short/Int/Long to declare Enum, it will get compilation error.

Root Cause
It's by design. Using value type alias is required exactly in Enum declaration.

It is "by design".

  1. The syntax is correct. C# specification explicitly states that the enum's underlying type must be byte, sbyte, short, ushort, int, uint, long or ulong.

  2. Although the underlying type of “Short” and "System.Int16" are one and the same, they are not as identical as you assume. The System.Int16 is a type, and the Short is a type keyword.

  3. ValueType is sealed. You cannot inherit from any class that derives from System.ValueType.

  4. But, it allows you to use the keyword to control. The intent is to prevent your using names of any types that derive from System.ValueType when declaring other types. The original intent is to provide a tightly controlled mechanism that allows you to declare types that inherit from System.ValueType. However, MSDN says "The C# type keywords and their aliases are interchangeable", which often let customers feel confused/inexplicable. They think it’s a compiler or grammar bug.

Business Impact / Customer Experience
Since the “Short” and "System.Int16" are the same one underlying type and can be interchangeable, many customers feel confused/inexplicable why it cannot be interchangeable in Enum declaration. They think it’s a compiler or grammar bug. The Voice of customers: This sounds like a small compiler bug (small becuase its easy to workaround). Here is what MSDN says "The C# type keywords and their aliases are interchangeable." In my view this is a real but tiny, compiler (or perhaps grammar) bug. The fact that the compiler lets you use "short" here and not "System.Int16" is a quirk of the compiler (perhaps not a bug, but a quirk).

like image 169
Ed S. Avatar answered Oct 04 '22 07:10

Ed S.