Using system;using system.collections.generic;using system.linq;using system.text;using System.Collections; namespace consoleapplication1{//enum declaration: Access rhetorical character enum enum name: base type public enum Enumname:int//This int can omit {enum into Member}//The underlying type must be able to represent all enumerated values defined in the enumeration. An enumeration declaration can explicitly declare a byte, sbyte, short, ushort, int, uint, long, or ulong type as the corresponding underlying type. An enumeration declaration that does not explicitly declare an underlying type means that the corresponding underlying type is int. The default underlying type of an enumeration element is int.By default. The first enumerator has a value of 0, and the value of each subsequent enumerator increments by 1. Enum days {Sat, Sun, Mon, Tue, Wed, Thu, Fri}; ↑//This default value is enum days{0,1,2,3,4,5,6}, if you want to specify a default value of 1, you must enum days{stat=1,sun=2, ...} An enumeration member is a named constant of the enumeration type. Arbitrarily two enumeration members cannot have the same name. Each enumeration member has an associated constant value.
The type of this value is the underlying type of the enumeration.
The constant value of each enumeration member must be within the range of the underlying type of the enumeration. Enum Gender:uint {//Male = -3, Female =-2, unkown =-1//generates a compile-time error. The reason is that the constant value-1,-2, and –3 are not in the range of the base integral uint. }; Defines an enumeration. This enumeration has three values of moning, afternoon, Evening enum TimeOfDay {moning, afternoon, Evening}; The value of the morning value of 0,afternoon is 2 for the value of 1,evening. Class Program {public static string Week (TimeOfDay str)//defines a Week method. It has a reference to an enumeration type (TimeOfDay) str {string result = string. Empty; The default value for switch (str) {//timeofday.moning is 0 case timeofday.moning: result = "Morning"; Break Case TimeOfDay.Afternoon:result = "PM"; Break Case TimeOfDay.Evening:result = "Night"; Break } return result; } static void Main (string[] args) {string s = Program.week (timeofday.moNing); Console.WriteLine (s); Output morning Console.readkey (); } }}
C # Enumeration