C # enumeration enum

Microsoft's first official reference paste: enum (C # Reference)

definition

Unique type enum keyword is used to declare an enumeration, that is called by a group of named constants enumerator list composed. Typically, it is best defined directly in the namespace enumerated, so that all the classes in this namespace can be equally easily access it. You may be enumerated in a class or nested structure.

Enumeration is a value type. The name can not contain spaces.

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

In the case of the above definitions, that is the value of each enumerator usual default, the first enumerator is 0, 1 back in ascending order. Of course, we can also use initializer to override the default value, as shown in the following example.

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

In this example, a force from the start sequence.

Enumeration value acquisition

1. Iterating: the static method Enum used directly in the operation .GetValues to the value obtained in the enumeration, enumeration type name, after the name of the enumeration is automatically output
foreach (int i in Enum.GetValues(typeof(Day))) Console.WriteLine(i);
2. Get the value enumerator
Day.Sat

Enumerated type

Each enum type has a base type, this foundation is an integer type other than char, the default is int.

When you declare an enumerated type should look like the following example:
enum Day : byte {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

Enumerated advantages

1. enumeration can make your code easier to maintain, to help ensure that the specified variable legitimate, desired value.
Enumerate make code clearer, allowing the integer value by a descriptive name, rather than cryptic numbers to represent.
3. enumeration makes code easier to type. When assigned to an instance of an enumerated type, VS.NET IDE through IntelliSense will pop up a list box that contains acceptable values, reduce the number of keystrokes, and can let us recall the possible values

Guess you like

Origin www.cnblogs.com/halfsaltedfish/p/11355259.html