Iterating the values of an enum (enumeration type) in C#
Use this generic helper function to get the values of the enum:
static T[] GetValuesOfEnum<T>() where T : struct, Enum => (T[]) Enum.GetValues(typeof(T));
Example:
enum Color
{
Red, Green, Blue
}
void PrintColors()
{
foreach(var color in GetValuesOfEnum<Color>())
{
Console.WriteLine(color);
}
}
Reference:
https://stackoverflow.com/questions/105372/how-to-enumerate-an-enum
Comments
Post a Comment