Why are position markers, such as the first or last, listed in the revised practice?

According to the Best Practices section of the MSDN documentation for the System.Enum class:

Do not specify the value of an enumeration just to reflect the state of the enumeration itself. For example, do not define an enumerated constant that simply marks the end of the enumeration. If you need to determine the last value of an enumeration, check this value explicitly. In addition, you can perform a range check for the first and last enumerated constant if all values ​​within the range are valid.

If I understand correctly, we should not declare the listing as follows.

public enum DrawOrder
{
    VeryBottom = 0,
    Bottom = 1,
    Middle = 2,
    Top = 3,
    Lowest = VeryBottom, //marks a position in the enum
    Highest = Top, //marks a position in the enum
}

Why is this considered bad practice?

+5
source share
4

. , ( ) DrawOrder.Highest //- .

.

.

DrawOrder enum , DrawOrder.Highest. , - DrawOrder.Highest .

, ?

+2

, , ; .

Code Complete . ( , , , , .)

, , Highest / Lowest, . , .

, , , .

+2

, VeryTop = 4 , Highest.

, . ( ).

+2

, , Enum, , . , , , , . , ... .

, , . Top, Highest, SuperTop Highest , .

, , , .

So many people find this a bad practice because it’s very convenient to use a marker when you intend to get a specific value.

+1
source

All Articles