c#枚举数字转枚举_C#中的枚举(枚举)

深藏阁楼爱情的钟 2023-03-05 11:58 72阅读 0赞

c#枚举数字转枚举

enum stands for enumeration, it is a set of named integer constants, their default integer values start with 0, we can also set any other sequence of the values.

enum代表枚举 ,它是一组命名的整数常量,它们的默认整数值以0开头,我们还可以设置任何其他序列的值。

An enum is defined by using the enum keyword.

枚举是使用enum关键字定义的。

Syntax:

句法:

  1. enum enum_name {enumeration_list };

Example 1:

范例1:

Here, we are defining an enum named colors with the three constants RED, GREEN and BLUE, we are not initializing them with any value. Thus, constant will have values 0, 1 and 2.

在这里,我们使用三个常量RED , GREEN和BLUE定义了一个名为colors的枚举 ,我们没有使用任何值对其进行初始化。 因此,常数将具有值0,12。

  1. using System;
  2. using System.Text;
  3. namespace Test
  4. {
  5. class Program
  6. {
  7. //declaring enum
  8. enum colors {
  9. RED, GREEN, BLUE };
  10. static void Main(string[] args)
  11. {
  12. //printing values
  13. Console.WriteLine("Red: {0}", (int)colors.RED);
  14. Console.WriteLine("Green: {0}", (int)colors.GREEN);
  15. Console.WriteLine("Blue: {0}", (int)colors.BLUE);
  16. //hit ENTER to exit
  17. Console.ReadLine();
  18. }
  19. }
  20. }

Output

输出量

  1. Red: 0
  2. Green: 1
  3. Blue: 2

Example 2:

范例2:

Here, we are defining an enum named colors with the three constants RED, GREEN and BLUE, we are initializing RED with 10, GREEN with 20 and BLUE is not getting initialized with any value, so it will take 21. Thus, constant will have values 10, 20 and 21.

在这里,我们用三个常量RED , GREEN和BLUE定义了一个名为colors的枚举 ,我们用10初始化RED ,用20初始化GREEN ,并且BLUE没有初始化任何值,所以它将花费21 。 因此,常数将具有值10,2021。

  1. using System;
  2. using System.Text;
  3. namespace Test
  4. {
  5. class Program
  6. {
  7. //declaring enum
  8. enum colors {
  9. RED = 10, GREEN = 20, BLUE };
  10. static void Main(string[] args)
  11. {
  12. //printing values
  13. Console.WriteLine("Red: {0}", (int)colors.RED);
  14. Console.WriteLine("Green: {0}", (int)colors.GREEN);
  15. Console.WriteLine("Blue: {0}", (int)colors.BLUE);
  16. //hit ENTER to exit
  17. Console.ReadLine();
  18. }
  19. }
  20. }

Output

输出量

  1. Red: 10
  2. Green: 20
  3. Blue: 21

Example 3: Printing enum’s constant name & value using foreach loop

示例3:使用foreach循环打印枚举的常量名称和值

Here, we are using Enum.GetValues(typeof(Days) which returns an array of all enum values and to get the name of enum, we are using Enum.GetName(typeof(Days), day). Here, Days is the enum name and day is the index.

在这里,我们使用Enum.GetValues(typeof(Days) ,它返回所有枚举值的数组,并获取enum的名称,我们在使用Enum.GetName(typeof(Days),day) 。这里, Days是枚举名称和日期是索引。

  1. using System;
  2. using System.Text;
  3. namespace Test
  4. {
  5. class Program
  6. {
  7. //declaring enum
  8. enum Days {
  9. SUN, MON, TUE, WED, THU, FRE, SAT };
  10. static void Main(string[] args)
  11. {
  12. //printing enum using foreach loop
  13. foreach (int day in Enum.GetValues(typeof(Days)))
  14. {
  15. Console.WriteLine("Name: {0}, Value: {1}", Enum.GetName(typeof(Days), day), (int)day);
  16. }
  17. //hit ENTER to exit
  18. Console.ReadLine();
  19. }
  20. }
  21. }

Output

输出量

  1. Name: SUN, Value: 0
  2. Name: MON, Value: 1
  3. Name: TUE, Value: 2
  4. Name: WED, Value: 3
  5. Name: THU, Value: 4
  6. Name: FRE, Value: 5
  7. Name: SAT, Value: 6

翻译自: https://www.includehelp.com/dot-net/enumeration-enum-in-c-sharp.aspx

c#枚举数字转枚举

发表评论

表情:
评论列表 (有 0 条评论,72人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C#——

    枚举 ![0_13315183659D8L.gif][] > 声明枚举的条件:确定数量,确定值的取值范围。 > > 枚举的语法:(1)声明枚举的时候和类同级。 > >

    相关 C++

    1. 介绍 第一次学习枚举类型时,觉得这个名字很诡异。但是后来发现,“枚举”真的特别传神,枚举就是可数的意思。 当你发现某个类型的值是数得过来的,那就派枚举出场吧。

    相关 C#】

    简单总结一下枚举的用法: 什么是枚举? 枚举简单的说是一种数据类型,只不过这种数据类型只包含自定义的特定数据,它是一组有共同特性的数据的集合。举个例子,颜色也可以