枚举(或枚举)是C#中的值数据类型。它主要用于将名称或字符串值分配给整数常量,从而使程序易于阅读和维护。例如,一副扑克牌中的4个西服可以是名为Club,Diamond,Heart和Spade的4个枚举器,属于名为Suit的枚举类型。其他示例包括自然枚举类型(例如行星,星期几,颜色,方向等)。枚举的主要目的是定义我们自己的数据类型(枚举数据类型)。直接在名称空间,类或结构内部使用enum关键字声明枚举。
句法:
enum Enum_variable
{
string_1...;
string_2...;
.
.
}
在上面的语法中,Enum_variable是枚举数的名称,string_1附加值0,string_2附加值1,依此类推。因为默认情况下,枚举的第一个成员的值为0,并且每个连续的枚举成员的值加1。我们可以更改此默认值。
- 示例1:考虑下面的枚举代码。在这里创建了名称为month的枚举,其数据成员为月份的名称,如jan,feb,mar,apr,may。现在,让我们尝试打印这些枚举的默认整数值。要从枚举类型转换为整数类型,需要显式转换。
// C# program to illustrate the enums // with their default values using System; namespace ConsoleApplication1 { // making an enumerator 'month' enum month { // following are the data members jan, feb, mar, apr, may } class Program { // Main Method static void Main(string[] args) { // getting the integer values of data members.. Console.WriteLine("The value of jan in month " + "enum is " + (int)month.jan); Console.WriteLine("The value of feb in month " + "enum is " + (int)month.feb); Console.WriteLine("The value of mar in month " + "enum is " + (int)month.mar); Console.WriteLine("The value of apr in month " + "enum is " + (int)month.apr); Console.WriteLine("The value of may in month " + "enum is " + (int)month.may); } } }
输出:The value of jan in month enum is 0 The value of feb in month enum is 1 The value of mar in month enum is 2 The value of apr in month enum is 3 The value of may in month enum is 4
- 示例2:在下面的代码中,将创建带有名称形状的枚举器,并将字符串数据成员设置为Circle ,默认情况下,其初始化为值0,类似地, Square在Perimeter类中分配为值1。还有一个成员函数peri(),它将一个参数作为值来初始化边/半径。另一个参数用于以整数值(0或1)的形式判断形状是圆形还是正方形。现在在main()方法中,创建了Perimeter类的对象。在调用peri()方法期间, Perimeter.shapes.circle表示它是一个值为0的圆,类似的情况是Perimeter.shapes.square的值为1。因此,在方法内部,如果s1对象的值为0那么它是圆形,因此计算出它的周长,正方形也是如此
周长。// C# program to illustrate the Enums using System; namespace ConsoleApplication2 { class Perimeter { // declaring enum public enum shapes { circle, square } public void peri(int val, shapes s1) { // checking for shape to be circle if (s1 == 0) { // Output the circumference Console.WriteLine("Circumference of the circle is " + 2 * 3.14 * val); } else { // else output the perimeter of the square Console.WriteLine("Perimeter of the square is " + 4 * val); } } } class Program { // Main Method static void Main(string[] args) { Perimeter a1 = new Perimeter(); a1.peri(3, Perimeter.shapes.circle); a1.peri(4, Perimeter.shapes.square); } } }
输出:Circumference of the circle is 18.84 Perimeter of the square is 16
枚举的初始化:如上所述,第一个枚举成员的默认值设置为0,对于枚举的其他数据成员,其默认值增加1。但是,用户也可以更改这些默认值。
- 例子:
enum days { day1 = 1, day2 = day1 + 1, day3 = day1 + 2 . . }
在上面的示例中,day1被用户分配了值“ 1”,day2将被分配了值“ 2”,并且day3成员的情况与此类似。因此,您只需要更改枚举的第一个数据成员的值,枚举的其他数据成员将自动比前一个增加1。
注意:现在,如果enum成员的数据成员尚未初始化,则其值将根据以下规则设置:
- 如果它是第一个成员,则将其值设置为0,否则
- 列出将枚举数据成员的先前值加1所获得的值
- 例子:
enum random { A, B, C = 6; D }
在这里,默认情况下A设置为0,B将递增为1。但是,由于C初始化为6,所以D的值为7
- 程序:用用户定义的值演示枚举数据成员的初始化,以及一些特殊的初始化情况。
// C# program to illustrate the enum // data member Initialisation using System; namespace ConsoleApplication3 { // enum declaration enum days { // enum data members monday, tuesday, wednesday, thursday, friday, saturday, sunday } // enum declaration enum color { // enum data members Red, Yellow, Blue, // assigning value yellow(1) + 5 Green = Yellow + 5, Brown, // assigning value Green(6) + 3 Black = Green + 3 } class Program { // Main Method static void Main(string[] args) { Console.WriteLine("Demostrating the difference "+ "between Special Initialisation" + "cases and non-initialisation cases\n\n"); // first of all non-initialised enum // 'days' will be displayed // as mentioned already, the first // member is initialised to 0 // hence the output will numbers // from 0 1 2 3 4 5 6 Console.WriteLine("Value of Monday is " + (int)days.monday); Console.WriteLine("Value of Tuesday is " + (int)days.tuesday); Console.WriteLine("Value of Wednesday is " + (int)days.wednesday); Console.WriteLine("Value of Thursday is " + (int)days.thursday); Console.WriteLine("Value of Friday is " + (int)days.friday); Console.WriteLine("Value of Saturday is " + (int)days.saturday); Console.WriteLine("Value of Sunday is " + (int)days.sunday); // Now the use of special initialisation // cases is demostrated as expected Red // will be assigned 0 value similarily // yellow will be 1 and blue will be 2 // however, green will be assigned the // value 1+5=6 similarily is the case // with brown and black Console.WriteLine("\n\nColor Enum"); Console.WriteLine("Value of Red Color is " + (int)color.Red); Console.WriteLine("Value of Yellow Color is " + (int)color.Yellow); Console.WriteLine("Value of Blue Color is " + (int)color.Blue); Console.WriteLine("Value of Green Color is " + (int)color.Green); Console.WriteLine("Value of Brown Color is " + (int)color.Brown); Console.WriteLine("Value of Black Color is " + (int)color.Black); } } }
输出:Demostrating the difference between Special Initialisation cases and non-initialisation cases Value of Monday is 0 Value of Tuesday is 1 Value of Wednesday is 2 Value of Thursday is 3 Value of Friday is 4 Value of Saturday is 5 Value of Sunday is 6 Color Enum Value of Red Color is 0 Value of Yellow Color is 1 Value of Blue Color is 2 Value of Green Color is 6 Value of Brown Color is 7 Value of Black Color is 9
说明:在上面的代码中,我们形成了两种枚举类型,即color和days 。如果是枚举数天,则不会进行初始化。因此,按照规则,星期一将被分配为0,增量为1,将确定星期二,星期三和其他日期的值。但是,在枚举颜色的情况下,红色将被分配0,黄色将被赋值为1,蓝色则被赋值。但是,如果是绿色,则其值将通过将黄色的值与5的值相加来确定,从而得出值6。同样,如果是棕色,则其值为7,如果是黑色,则其值为(7 + 3)是10。
更改Enum数据成员的类型:默认情况下,C#中枚举器的基本数据类型为int 。但是,用户可以根据需要更改它,例如bool,long,double等。
- 例子:
// byte type enum button : byte { // OFF will be assigned 0 OFF, //ON will be assigned 1 ON // However, if we assign 100 to ON then, // this will give error as byte cannot hold this }
- 程序:演示枚举成员数据类型的更改
// C# program to illustrate the changing // of data type of enum members using System; namespace ConsoleApplication4 { // changing the type to byte using : enum Button : byte { // OFF denotes the Button is // switched Off... with value 0 OFF, // ON denotes the Button is // switched on.. with value 1 ON } class Program { // Main Method static void Main(string[] args) { Console.WriteLine("Enter 0 or 1 to know the " + "state of electric switch!"); byte i = Convert.ToByte(Console.ReadLine()); if (i == (byte)Button.OFF) { Console.WriteLine("The electric switch is Off"); } else if (i == (byte)Button.ON) { Console.WriteLine("The electric switch is ON"); } else { Console.WriteLine("byte cannot hold such" + " large value"); } } } }
输入:
1
输出:
Enter 0 or 1 to know the state of electric switch! The electric switch is ON