Why don't enumerations with default values ​​work properly in C #?

I went through the Jon skeet website in C # Brain teasers http://www.yoda.arachsys.com/csharp/teasers.html . Why is the "Baz" element displayed on the output, although I have declared default values ​​for all the elements in the enumeration

--- for example: 1

class Test { enum Foo { Bar, Baz,bread, jam }; const int One = 1; const int Une = 1; static void Main() { Foo f = 0; Console.WriteLine(f); Console.ReadLine(); } } // output :Bar 

- EG2

 class Test { enum Foo { Bar, Baz,bread=0, jam }; const int One = 1; const int Une = 1; static void Main() { Foo f = 0; Console.WriteLine(f); Console.ReadLine(); } } //output : Bar 

- EG3

 class Test { enum Foo { Bar, Baz=0, bread=0, jam }; const int One = 1; const int Une = 1; static void Main() { Foo f = 0; Console.WriteLine(f); Console.ReadLine(); } } //output :Baz 

- EG4

 class Test { enum Foo { Bar=0, Baz=0, bread=0, jam=0}; const int One = 1; const int Une = 1; static void Main() { Foo f = 0; Console.WriteLine(f); Console.ReadLine(); } } //output:Baz 
+5
source share
3 answers

Enumerations are integers in .NET. Every time you think of them as a name (for example, ToString() , you actually say: “Try to find a specific label enum that matches an integer value, and tell me its name.” In this case, you have several renamed labels with the same integer value, so the name in WriteLine is undefined.Note that WriteLine here ultimately does f.ToString() , which applies the “find value” logic above.

For --eg5 , I would suggest: Foo f = (Foo)-1327861; . Perfectly valid in .NET terms, but does not meet the definition of an enumeration. It's not obligatory.

+4
source

You cannot set default values ​​for your enumeration members, only for a full enumeration. The default value for it is 0 , which refers to the first element of the enumeration. Other members of the enumeration must differ from this value, otherwise they simply redefine it. In this case:

 enum Foo { Bar=0, Baz=0, bread=0, jam=0 }; 

You tell the compiler: OK, now they will be called 0 as Bar . So now 0 will be called Baz and so on. This makes no sense to the compiler.

In the Code Complete book, you can find advice on introducing a default enumeration member named None and explicitly assign it 0 and put it in the first place of your enumeration. So your code might look like this:

 enum Foo { None = 0, Bar, // 1 Baz, // 2 bread, // 3 jam // 4 }; 
+1
source

Consider the following example:

 enum Foo { Bar, Baz,bread=0, jam=0}; public static void Main() { Console.WriteLine((int)Foo.Bar); // 0 Console.WriteLine((int)Foo.Baz); // 1 Console.WriteLine((int)Foo.bread); // 0 Console.WriteLine((int)Foo.jam); // 0 } 

So, we can say that if we do not assign any integer values ​​to the first element in the enumeration, it will be assigned the value 0 , and the next element will be the previous item value + 1 , if you assign some values, then it will overwrite the values, increasing by default

Take a look at this example.

0
source

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


All Articles