该GetName()
方法返回Enumeration中常量的名称。
这是枚举。
enum Stock { Appliance, Clothing, Footwear };
现在,使用Enum.GetName()方法获取名称。只需设置常量并获取个人名称即可。
Enum.GetName(typeof(Stock), 1
现在让我们来看一个例子。
using System; class Demo { enum Stock { Appliance, Clothing, Footwear }; static void Main() { Console.WriteLine("The value of second stock category = {0}",Enum.GetName(typeof(Stock), 1)); Console.WriteLine("The value of third stock category = {0}",Enum.GetName(typeof(Stock), 2)); } }
输出结果
The value of second stock category = Clothing The value of third stock category = Footwear