在GetNames()
返回的枚举常量的名称数组。
以下是枚举。
enum Stock { Watches, Books, Grocery };
要获取名称数组,请使用GetNames()
and循环,如下所示-
foreach(string s in Enum.GetNames(typeof(Stock))) { }
现在让我们来看完整的示例。
using System; class Demo { enum Stock { Watches, Books, Grocery }; static void Main() { Console.WriteLine("The value of first stock category = {0}",Enum.GetName(typeof(Stock), 0)); 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)); Console.WriteLine("All the categories of stocks..."); foreach(string s in Enum.GetNames(typeof(Stock))) { Console.WriteLine(s); } } }
输出结果
The value of first stock category = Watches The value of second stock category = Books The value of third stock category = Grocery All the categories of stocks... Watches Books Grocery