An
enumeration type is a value type defined by a set of named constants of the underlying integral
numeric type . By default, the associated constant values of enum members are of
text type int; they start with zero and increase by one following the definition text order. The following code shows How to enumerate an
enum in C# .
using System;
class HelloWorld {
enum Numbers
{
One,
Two,
Three,
Four
};
public static void Main() {
foreach (Numbers number in (Numbers[]) Enum.GetValues(typeof(Numbers)))
{
System.Console.WriteLine(number);
}
}
}
output
One
Two
Three
Four
How to loop through all enum values in C#?
using System;
class HelloWorld {
enum Numbers
{
One,
Two,
Three,
Four
};
public static void Main() {
foreach (var number in Enum.GetValues(typeof(Numbers)))
{
System.Console.WriteLine(number.ToString());
}
}
}
output
One
Two
Three
Four