显示枚举、枚举值、枚举名:
using System;
class MyClass
{
enum MyEnum { AA, BB, CC }; /* 类型定义不能在函数体内 */
static void Main()
{
MyEnum e = MyEnum.CC;
Console.WriteLine(e); // CC
Console.WriteLine((byte)e); // 2
Console.WriteLine((int)e); // 2
Console.WriteLine(Convert.ToString(e)); // CC
Console.ReadKey();
}
}
转换数值或字符串到枚举:
using System;
class MyClass
{
enum MyEnum { AA, BB, CC };
static void Main()
{
MyEnum e;
e = (MyEnum)1;
Console.WriteLine(e); // BB
e = (MyEnum)Enum.Parse(typeof(MyEnum), "BB");
Console.WriteLine(e); // BB
Console.ReadKey();
}
}
时间: 2025-01-19 04:55:02