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

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:

句法:

    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。

using System;
using System.Text;

namespace Test
{
    class Program
    {
        //declaring enum
        enum colors { RED, GREEN, BLUE };
        static void Main(string[] args)
        {            
            //printing values
            Console.WriteLine("Red: {0}", (int)colors.RED);
            Console.WriteLine("Green: {0}", (int)colors.GREEN);
            Console.WriteLine("Blue: {0}", (int)colors.BLUE);
			
            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

输出量

Red: 0
Green: 1
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。

using System;
using System.Text;

namespace Test
{
    class Program
    {
        //declaring enum
        enum colors { RED = 10, GREEN = 20, BLUE };
        static void Main(string[] args)
        {            
            //printing values
            Console.WriteLine("Red: {0}", (int)colors.RED);
            Console.WriteLine("Green: {0}", (int)colors.GREEN);
            Console.WriteLine("Blue: {0}", (int)colors.BLUE);
			
            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

输出量

Red: 10
Green: 20
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是枚举名称和日期是索引。

using System;
using System.Text;

namespace Test
{
    class Program
    {
        //declaring enum
        enum Days { SUN, MON, TUE, WED, THU, FRE, SAT };
        static void Main(string[] args)
        {
            //printing enum using foreach loop
            foreach (int day in Enum.GetValues(typeof(Days)))
            {
                Console.WriteLine("Name: {0}, Value: {1}", Enum.GetName(typeof(Days), day), (int)day);
            }
			
            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

输出量

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


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

c#枚举数字转枚举

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值