C # Enumeration (Enum) Summary
Enumeration is a set of named integer constants. Enumerated type is to use the enum keyword declared.
C # enumeration is a value type. In other words, the enumeration contains its own value and can not inherit or transfer inheritance.
Declare enum variables
Enum declaration of general syntax:
enum <enum_name> { enumeration list };
among them,
- enum_name specified enumeration type name.
- enumeration list is a comma-separated list of identifiers.
Each symbol represents an integer enumerated list of values, a greater than its preceding signed integer values. By default, the value of the first symbol of the enumeration is 0. For example:
enum Days { Sun , Mon , Tue , Wed , you , Fri , Sat };
Examples The following example demonstrates the use of an enumeration: Examples of the using the System; public class EnumTest { enum Day {the Sun, Mon, Tue, Wed, Thu, Fri, Sat}; static void the Main () { int X = ( int ) Day.Sun; int Y = ( int ) Day.Fri; Console.WriteLine ( " the Sun = {0} " , X); Console.WriteLine ( " Fri = {0} " , Y); } } when the above code when compiled and executed, it produces the following results: the Sun = 0 Fri = . 5
Enumeration value or a correlation implicitly or explicitly assigned. If the declaration enum member having a constant-expression initializer, the value of the constant expression (which implicitly convertible to type based enumeration) is associated with the enumerated value of the member. If the declaration of the enum member has no initializer, its associated value according to the following rules implicitly set:
If the enum member is the first enum member declared in enum type, its associated value is zero. Otherwise, the value of enum member is associated by a former member of enumeration (in text order) plus an associated value obtained. This increases the value must be within the range of values of the underlying type that can be represented; otherwise, a compile-time error occurs. Members of the association about enumerated type value is assigned the case, refer to the following example:
the using the System; namespace the Test { public enum Day: uint // If the data type is not provided, the default is int {Mon = . 1 , Tue = 2 , Wed = . 3 , Thu = . 4 , Fri = . 5 , Sat, the Sun} class EnumType { public static void the Main ( String [] args) { Console.WriteLine (ChooseDay (Day.Sun)); Console.WriteLine (ChooseDay (Day.Mon)); } static String ChooseDay (Day D) { String tmp = String .Empty; Switch (D) { Case Day.Mon: tmp = String .Format ( " Your Choice {0} day (Monday) of the week, work " , ( uint ) D); BREAK ; Case Day.Tue: tmp = String .Format ( " your choice {0} day of the week (i.e. Tuesday), work " , ( uint ) D); BREAK ; Case Day.Wed: tmp= String .Format ( " Your Choice {0} day of the week (i.e. Wednesday), work " , ( uint ) D); BREAK ; Case Day.Thu: tmp = String .Format ( " you select the first week } {0 days (i.e. Thursday), work " , ( uint ) D); BREAK ; Case Day.Fri: tmp = String .Format ( " your choice} {0 days (i.e., on Friday of the week), the working " , ( uint ) D); BREAK; Case Day.Sat: tmp = String .Format ( " Your Choice {0} day of the week (i.e., Saturday), the rest " , ( uint ) D); BREAK ; Case Day.Sun: tmp = String .Format ( " you have chosen {0} day of the week (i.e. on Sunday), the rest " , ( uint ) D); BREAK ; default : tmp = " invalid " ; BREAK ; } Return tmp; } } } copy the code // Execation the Result of your choice on the 7th day (ie Sunday), the rest of the week you choose the first day of the week (ie Monday), the working press any key to continue...
Call Enumeration
public enum CommunicationState { /// <Summary> /// Location Status /// </ Summary> Unknown = 0 , /// <Summary> /// waits for user login /// </ Summary> WaitforUser = . 1 , / // <Summary> /// waits for the user to enter a password /// </ Summary> WaitforPass = 2 , /// <Summary> /// waits for a user instruction /// </ Summary> WaitforCmd = . 3, /// <summary> /// 接收数据中 /// </summary> Receiving = 4, /// <summary> /// 断开连接 /// </summary> Disconnected = 5 }
private CommunicationState m_ComState = CommunicationState.Unknown;
this.m_ComState = CommunicationState.WaitforUser;