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

C# enumerate an enum

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

Enum.GetValues(Type) Method

GetValues(Type) retrieves an array of the values of the constants in a specified enumeration. The elements of the array are sorted by the binary values of the enumeration constants
Prev
Index Page
Next
©Net-informations.com