C# 枚举的使用

//定义枚举
public enum SkinEnum
{
    NotSkin = 0,//不使用IrisSkin皮肤
    Calmness = 1,
    CalmnessColor1 = 2,
    CalmnessColor2 = 3,
    DeepCyan = 4,
}

//遍历枚举
foreach (SkinEnum s in Enum.GetValues(typeof(SkinEnum)))
{
    comboBox1.Items.Add(s);
}  

//字符串转枚举
skinEngineHelper.SetSkinFile((SkinEnum)Enum.Parse(typeof(SkinEnum), comboBox1.Text));





//定义枚举
public enum Demo
{
    Moning = 0,
    Afternoon = 1,
    Evening = 2,
};


//获取枚举名称字符串
Demo time = Demo.Afternoon;
Console.WriteLine(time.ToString());//输出:Afternoon

//字符串转枚举
Demo time2 = (Demo) Enum.Parse(typeof(Demo), "afternoon", true);
Console.WriteLine((int)time2);//转int输出1

//枚举的某一值对应名称
lbOne.Text = Enum.GetName(typeof(Demo), 0);//如果越界返回空字符串
lbOne.Text = ((Demo)0).ToString();//返回:Morning;如果越界返回查询的int原值

//遍历枚举的所有的值
foreach (int i in Enum.GetValues(typeof(Demo)))
            lbValues.Text += i.ToString();

//遍历所有的名称
foreach(string temp in Enum.GetNames(typeof(Demo)))
            lbNames.Text+=temp;

//判断变量是否存在枚举中
if (Enum.IsDefined(typeof(Demo), strName))//指定枚举中是否存在具有指定值的常数的指示
{
    //
}
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页