C # Creates an enumeration type that uses the enum keyword to limit its value to only a set of symbolic names.
I. Declaration Enumeration
Define an enumeration to write an enum keyword, followed by {}, and then add a set of symbols within {} that identify the legal values that the enumeration type can have. For example:
enum Week {Monday, Tuesday, Wednesday, Thrusday, Friday, Saturday, Sunday}// Declaration One-week enumeration
Ii. Use of enumerations
Once declared, you can use them just like any other type, which declares a week enumeration below which we use it. For example
Week weekday = week. Monday;//This position is equivalent to declaring a local variable, storing the Monday intA = Convert.ToInt32 (weekday);//a = week. Monday int type Data//Try//{ //int a = Convert.ToInt32 (weekday); //Console.WriteLine (a); //} //catch (system.formatexception)//{ //Console.WriteLine ("Malformed"); //}Console.WriteLine ("{0}\t{1}", weekday,a);
As with all value types, can you use modifiers? Creates a nullable enumeration variable. This can be done in addition to assigning the enumeration type definition to the variable, and it is OK to assign a null value to it. For example
Week weekday = week. Monday;//This position is equivalent to declaring a local variable, storing the MondayWeek? Weekdaytest =NULL;//use? Modifies the weekdaytest variable and assigns a value of NULLConsole.WriteLine (weekdaytest);//output NULLConsole.WriteLine (Convert. ToInt32 (weekdaytest));//convert null (weekdaytest) to type intWeekdaytest = week. Sunday;//assign weekdaytest to Week.sundayConsole.WriteLine (weekdaytest); intA = Convert.ToInt32 (weekday);//a = week. Monday int type Data//Try//{ //int a = Convert.ToInt32 (weekday); //Console.WriteLine (a); //} //catch (system.formatexception)//{ //Console.WriteLine ("Malformed"); //}Console.WriteLine ("{0}\t{1}", Weekday,a); Console.ReadLine ();
It is important to note that when using Console.WriteLine to display enumeration variables, the compiler automatically generates code, output strings that match the values of the variables, and, if necessary, call the ToString method for each enumeration, which shows that the enumeration variable is converted to a string representing its current worth.
Third, select the literal value of the enumeration
Each element inside the enumeration is associated (corresponding) with an integer value. The default first element corresponds to the integer 0, and the integer corresponding to each element is incremented by 1. The data can be converted from one type to another, so long as the conversion result is valid, meaningful conversions succeed.
Static voidMain (string[] args) {Week weekday= Week. Monday;//This position is equivalent to declaring a local variable, storing the MondayWeek? Weekdaytest =NULL;//use? Modifies the weekdaytest variable and assigns a value of NULLConsole.WriteLine (weekdaytest);//output NULLConsole.WriteLine (Convert. ToInt32 (weekdaytest));//convert null (weekdaytest) to type intWeekdaytest = week. Sunday;//assign weekdaytest to Week.sundayConsole.WriteLine (weekdaytest); Console.WriteLine (Convert.ToInt32 (Weekdaytest));//convert null (weekdaytest) to type int intA = Convert.ToInt32 (weekday);//a = week. Monday int type Data//Try//{ //int a = Convert.ToInt32 (weekday); //Console.WriteLine (a); //} //catch (system.formatexception)//{ //Console.WriteLine ("Malformed"); //}Console.WriteLine ("{0}\t{1}", Weekday,a); Console.ReadLine (); }
Note: The integer value used to initialize the enumeration literal must be a constant value that can be determined when the compiler compiles.
Iv. selecting the underlying type of enumeration
When declaring an enumeration, the enumeration literal is the int type by default, but you can make the enumeration type based on different base shaping types. Can be: Byte (max:255), SByte (max:127), Short,ushrot,int,uint,long,ulong. For example
enum week:sbyte {monday=1, Tuesday, Wednesday, Thrusday, Friday, Saturday, Sunday}//
Declaration One-week enumeration
Use of C # enumerations