Unexpected output when printing enumeration values

I have code that takes a parameter ulong, and using the mapping dictionary maps the ulong value to the value. However, the enumeration contains values ​​that are the same for all purposes, so they are reassigned later in the enumeration in the same way as AfricanMangoin the following enumeration:

public enum Fruits
{
    Banana = 0,
    Strawberry,
    Apple,
    Peach,
    Mango,
    Berry,

    AfricanMango = Mango,

    Cranberry
}

This should tell the compiler to use the same values ​​for AfricanMangoand Mangowhether this implementation really makes sense from a design point of view up.

My code converted the input to an enumeration value just fine, and when evaluating the result of a function, it returned Fruits.Cranberry. That was the value that I was expecting.

( Fruits.Cranberry), Fruits.Berry, , :

var fruit = Fruits.Cranberry;

Console.WriteLine(fruit);

, Cranberry, , , Berry.

? AfricanMango - ?

+4
2

enum, . , AfricanMango , Mango, , Mango + 1, Berry.

MSDN:

0, 1

+6

, . + 1. . Reflector:

public enum Fruits
{
    AfricanMango = 4,
    Apple = 2,
    Banana = 0,
    Berry = 5,
    Cranberry = 5, // <-- uch
    Mango = 4,
    Peach = 3,
    Strawberry = 1
}

. , .

MSDN:

0, 1. , Sat 0, Sun 1, Mon 2 ..

+5

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


All Articles