In code example shows how to loop through all enum values in C#.
enum Colors { Red, Orange, Green, Blue, Black};
enum Fruits { Apple = 1, Banana = 2, Orange = 3, Peer = 4 };
To get all values of an enum, we can use the Enum.GetValues static method. The Enum.GetValues method returns an array of all enum values. The following code snippet loops through all values of an enum and prints them on the Console.
foreach (int i in Enum.GetValues(typeof(Colors)))
{
Console.WriteLine($" {i}" );
}
The Enum.GetName method returns the constant's name in the specified enum with the specified value. The GetName method takes two arguments: the type of the enum and the index.
Console.Write($"{Enum.GetName(typeof(Colors), i)}");
The Enum.GetNames static method returns an array of the names of the constants in a specified enum. The following code snippet loops through an enum and prints all of its member names.
foreach (string name in Enum.GetNames(typeof(Fruits)))
{
Console.WriteLine(name);
}
Let's take another example where we use a .NET enum, ConsoleColor. The following code snippet loops through, get ConsoleColor enum values and sets the value as the Console's foreground color.
foreach (ConsoleColor color in Enum.GetValues(typeof(ConsoleColor)))
{
Console.ForegroundColor = color;
Console.WriteLine($"Foreground color set to {color}");
}
The complete C# enum code example is listed in the following Listing 1.
using System;
public class GetEnumValuesSample {
enum Colors {
Red,
Orange,
Green,
Blue,
Black
};
enum Fruits {
Apple = 1, Banana = 2, Orange = 3, Peer = 4
};
public static void Main() {
Console.WriteLine("The values of the Colors Enum are:");
foreach(int i in Enum.GetValues(typeof(Colors))) {
Console.Write($ "{Enum.GetName(typeof(Colors), i)}");
Console.WriteLine($ " {i}");
}
Console.WriteLine();
// Get names of
foreach(string name in Enum.GetNames(typeof(Fruits))) {
Console.WriteLine(name);
}
//Let's go through all Console colors and set them as foreground
foreach(ConsoleColor color in Enum.GetValues(typeof(ConsoleColor))) {
Console.ForegroundColor = color;
Console.WriteLine($ "Foreground color set to {color}");
}
Console.ReadKey();
}
}
Learn more here: Top 7 C# Enum Code Examples.

ChungusPosted Oct 28, 2023, 8:00 AM
First of all, your English is hard to follow, and you have made a number of syntax errors. I would have imagined a CEO to be able to use correct English in an article, even if you are not a native speaker. Secondly, it is customary to show the output of the examples. IE. enum Colors { Red, Orange, Green, Blue, Black}; foreach (int i in Enum.GetValues(typeof(Colors))) Console.WriteLine($" {i}" ); // output here This is very basic stuff when writing an article that contains lines of code.
Vikas ShindePosted Oct 24, 2019, 7:39 AM
Sir,Can You Explain, Why You had used the ''$'' Sign. Without $ we can do it.
Seshu Reddy LeburuPosted Sep 12, 2019, 9:04 AM
Nice explanation
Pankajkumar PatelPosted Aug 29, 2019, 12:37 AM
Nice article
dilip silgaPosted Jul 30, 2018, 4:11 AM
Nice sir thanks
Rajesh KumarPosted Jul 28, 2018, 4:03 AM
Nice article sir
Viknaraj ManogararajahPosted Jul 21, 2018, 10:46 PM
Nice Article, Thank you for sharing