C# | Type.GetEnumNames() Method

Type.GetEnumNames() Method is used to return the names of the members of the current enumeration type.

Syntax:

public virtual string[] GetEnumNames ();

Returns: This method returns an array which contains the names of the members of the enumeration.



Exception: This method will give ArgumentException if the current type is not an enumeration.

Below programs illustrate the use of the above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetEnumNames(Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Defining enum ABC
    enum ABC { A,
               B,
               C,
               D,
               E,
               F }
  
    // Main Method
    public static void Main()
    {
  
        // try-catch block to
        // handle exceptions
        try {
  
            // Creating and initializing object
            // of ABC with instace of enum ABC
            ABC a = ABC.A;
  
            // Declaring and initializing
            // object of Type
            Type type = a.GetType();
  
            // Getting Array of constants
            // By using GetEnumNames() method
            string[] obj = type.GetEnumNames();
  
            // Display Array obj
            Console.WriteLine("The constants are as follow :-");
            for (int i = 0; i < obj.Length; i++)
                Console.Write("{0} ", obj[i]);
        }
  
        // catch ArgumentException here
        catch (ArgumentException e)
        {
            Console.Write("The current type is not an enumeration.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

The constants are as follow :-
A B C D E F

Example 2: For ArgumentException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetEnumNames(Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Defining enum ABC
    enum ABC { A,
               B,
               C,
               D,
               E,
               F }
  
    // Main Method
    public static void Main()
    {
  
        // Creating try-catch block
        // to handle exceptions
        try {
  
            // Declaring and initializing
            // object of Type
            Type type = typeof(int);
  
            // Getting Array of constants
            // By using GetEnumNames() method
            string[] obj = type.GetEnumNames();
  
            // Display Array obj
            Console.WriteLine("The constants are as follow :-");
            for (int i = 0; i < obj.Length; i++)
                Console.Write("{0} ", obj[i]);
        }
  
        // catch ArgumentException here
        catch (ArgumentException e) 
        {
            Console.WriteLine("The current type is not an enumeration.");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

The current type is not an enumeration.
Exception Thrown: System.ArgumentException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.