首页 > 编程语言 >C#枚举的常用用法

C#枚举的常用用法

时间:2023-01-08 23:00:38浏览次数:39  
标签:Console C# myenu System 用法 枚举 WriteLine using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{   
    enum myenu
    {
        aaa = 0,
        bbb = 1,
        ccc = 2,
        ddd = 4
        // eee = "s0"  枚举的值只能为数字类型
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            // 枚举通过key获取value
            int sec = (int)myenu.aaa;
            Console.WriteLine(sec);

            // 枚举通过value获取key(感觉没啥用)
            myenu bbc = myenu.bbb;
            Console.WriteLine(Convert.ToString(bbc));

            // 枚举通过一个字符串获取枚举值
            string iii = "ddd";
            myenu xx = (myenu)Enum.Parse(typeof(myenu), iii);
            Console.WriteLine((int)xx);

            Console.ReadLine();
        }
    }
}

``

标签:Console,C#,myenu,System,用法,枚举,WriteLine,using
From: https://www.cnblogs.com/yingyingdeyueer/p/17035662.html

相关文章