Author : Admin
Last Modified : 12-Feb-2020
Complexity : Beginner

How to enumerate an enum in CSharp


Sometimes we need to enumerate the enum, suppose we have a Language Enum 

public enum Language
{
  CSharp,
  Java,
  JavaScript
  SQL,
  Python,
  PHP
}

Enum Class provides a static method name GetNames() which return the string array of the names of enum members. Below is the signature of the method

public static string[] GetNames(Type enumType);

So we use the GetNames method in our code

public void PrintAllLanguages()
{
    foreach (string name in Enum.GetNames(typeof(Language)))
    {
        System.Console.WriteLine(name);
    }
}