Enum with the specified values ​​only for some members

I have an enum with a custom value for only part of the list

 public enum MyEnum { FirstValue, SecondValue, ThirdValue, ForthValue = 1, FifthValue = 2 } 

When I tried strina name = (MyEnum)2; the name was ThirdValue .

But when I changed enum to

 public enum MyEnum { FirstValue = 3, SecondValue, ThirdValue, ForthValue = 1, FifthValue = 2 } 

In strina name = (MyEnum)2; the name was FifthValue .

Is the compiler (I'm using Visual Studio 2012) to initialize user values ​​only if the former have user values?

And if ThirdValue got the default value of 2 in the first example, why was there no error in FifthValue = 2 ?

+5
source share
3 answers

When you assign values ​​to an enumeration element, the compiler increments the value by one for the next member, unless it is defined. If none of the members matter, the numbering starts at 0.

Your first example, with what the compiler does:

 public enum MyEnum { FirstValue, // == 0 SecondValue, // == 1 ThirdValue, // == 2 ForthValue = 1, FifthValue = 2 } 

therefore you have two elements with a value of 2 .

Either specify all values, or do not give them any values. Everything else is likely to lead to confusion.

The C # standard, section 14.3 says (focus):

The associated value of an enumeration element is assigned either implicitly or explicitly. If the declaration of an enumeration element has a constant expression initializer, the value of this constant expression, implicitly converted to the base type of the enumeration, is the associated value of the enumeration member. If the declaration of the enumeration element does not have an initializer, its associated value is set implicitly, as shown below:

  • If an enumeration element is the first member of an enumeration element declared in an enumeration type, its associated value is zero .
  • Otherwise, the bound value of the enumeration element is obtained by increasing the bound value of the text preceding enumeration element by one . This increased value should be in the range of values ​​that can be represented by the base type; otherwise, a compile-time error occurs.
+6
source

The corresponding integer values ​​that enumerate map values ​​always start at 0 unless you specifically modify the values.

So your first code snippet is equivalent to this:

 public enum MyEnum { FirstValue = 0, SecondValue = 1, ThirdValue = 2, ForthValue = 1, FifthValue = 2 } 

So you can see that 2 cards correspond to both ThirdValue and FifthValue .

The second example is equivalent to this:

 public enum MyEnum { FirstValue = 3, SecondValue = 4, ThirdValue = 5, ForthValue = 1, FifthValue = 2 } 
+2
source

More than one counter may have the same value. If you do not want to specify a value for each enumerator, simply switch the order that defines the enumerations:

 public enum MyEnum { FourthValue = 1, FifthValue, FirstValue, SecondValue, ThirdValue } 
0
source

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


All Articles