Cannot implicitly use enum for integer in C #

I can define an integer enumeration as a value:

public enum FieldType 
{
    Bar = 0,
    Foo = 1
}

Why C # does not allow creating a dictionary with an integer as a key and using the enumeration value when creating:

public Dictionary<int, Type> _fieldMapping = new Dictionary<int, Type>
{
    {FieldType.Bar, null},  # Error in VS
    {FieldType.Foo, null},  # Error in VS
}

I know I can make cast, but I would like to know why enum cannot be converted to int implicitly when it really is an int under the hood.

+4
source share
1 answer

If you could do this, you would lose half the point of using enumerations in general - the point is to save them as separate types in order to avoid erroneous errors associated with improper use of types.

, FieldType - . , (int ) , , FileShare, .

, , , . , FieldType , 1 Type -? 1?

+8

Source: https://habr.com/ru/post/1662110/


All Articles