Enumerate an enum

Enum.GetValues method returns an array that contains a value for each member of the enumType enumeration.

https://msdn.microsoft.com/en-us/library/system.enum.getvalues(v=vs.110).aspx

enum Gender { Male=100, Female=200 };

// PrintAllGender()는 Male=100, Female=200 출력

public void PrintAllGender ()

{

foreach (Gender g in Gender.GetValues(typeof(Gender)))

{

Console.WriteLine(“{0}={1}”, g, (int)g);

}

}

// PrintByGender(..) 는 Male이면 남자, Female이면 여자 출력

public void PrintByGender (Gender g)

{

switch (g)

{

case Gender.Male:

Console.WriteLine(“남자”);

break;

case Gender.Female:

Console.WriteLine(“여자”);

break;

default:

Console.WriteLine(“중성자??”);

break;

}

}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *