enum DaysOfWeek { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } var values = Enum.GetValues(typeof(DaysOfWeek)); foreach (var day in values) { Console.WriteLine(day); }
enum Colors { Red = 1, Green = 2, Blue = 3 } var values = Enum.GetValues(typeof(Colors)); foreach (var color in values) { Console.WriteLine($"{color}={Convert.ToInt32(color)}"); }In this example, we have declared an enumeration type called Colors with assigned values. We are using the System.GetValues() method to retrieve an array of values from this enumeration and assigning it to the variable values. Then we are iterating over the array and printing each value and its corresponding integer value to the console. These examples are part of the System namespace and commonly packaged in the .NET Framework Class Library.