C#枚举GetNames方法
在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
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短