How to loop through an enum in C#? (part 1)
Here you will learn how to enumerate or loop through an enum.
In C#, an enum is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example,
An enum can be looped through using
The following example gets the names of an enum using the
Here you will learn how to enumerate or loop through an enum.
In C#, an enum is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example,
WeekDays.Monday
is more readable than number 0 when referring to the day in a week.An enum can be looped through using
Enum.GetNames<TEnum>()
, Enum.GetNames()
, Enum.GetValues<TEnum>()
, or Enum.GetValues()
static methods with the foreach loop.The following example gets the names of an enum using the
Enum.GetNames<TEnum>()
method.