GetNames()는 열거에 있는 상수 이름의 배열을 반환합니다.
다음은 열거입니다.
enum Stock { Watches, Books, Grocery }; 이름 배열을 얻으려면 GetNames()를 사용하고 아래와 같이 반복하십시오 -
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