怎样将int或者String类型转换为枚举

2025-01-05 10:40:08
推荐回答(1个)
回答1:

Enum.GetName(typeof(Days),4)

(Days)Enum.Parse(typeof(Days),4.ToString());

以下是msdn例子

enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

public static void Main()

{

Console.WriteLine("The entries of the Colors Enum are:");

foreach(string s in Enum.GetNames(typeof(Colors)))

Console.WriteLine(s);

Console.WriteLine();

Colors myOrange = (Colors)Enum.Parse(typeof(Colors), "4");

Console.WriteLine("The myOrange value has the combined entries of {0}", myOrange);

}