C# Code: List all enum values using foreach loop through an enum C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class Program { // Day enum represents number of days in a week enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; static void Main(string[] args) { foreach (Days day in Enum.GetValues(typeof(Days))) { Console.WriteLine(day); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 | Sunday Monday Tuesday Wednesday Thursday Friday Saturday |
C# Code: Get Enum Value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Program { // Day enum represents number of days in a week enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; static void Main(string[] args) { int something = (int)Days.Monday; Console.WriteLine(something); Console.ReadLine(); } } |
Output:
1 2 3 | 1 |