Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - C# explicit enumeration conversion problem
C# explicit enumeration conversion problem

Because you assigned an initial value to Green, the value defined after Green is the value of Green + 1, such as

enum Color

{

Red,

Green=10,

Blue,

Yellow,

Black =20,

White

}

Yellow is 12, White is 21, the following is the definition of enum in MSDN

The enum keyword is used to declare an enum An enumeration is a unique type consisting of a set of named constants called an enumerator list. Each enum type has a base type, which can be any integer except char. The default underlying type of enumeration elements is int. By default, the first enumerator has a value of 0, and each subsequent enumerator has a value incremented by 1. For example:

enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, Sat is 0, Sun is 1, and Mon is 2, and so on. Enumerators can have initializers that override the default value. For example:

enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, force the sequence of elements to start from 1 instead of 0 start.

A variable of type Days can be assigned any value within the range of the underlying type. The assigned value is not limited to named constants.

The default value of enum E is the value produced by the expression (E)0.

Note

The name of the enumerator cannot contain spaces.

The underlying type specifies the storage size allocated for each enumerator. However, conversion from an enum type to an integer type requires an explicit type conversion.

For example, the following statement assigns the enumerator Sun to a variable of type int by using a cast from enum to int:

int x = (int)Days.Sun;