C# | Type.GetEnumName() Method

Type.GetEnumName(Object) Method is used to return the name of the constant which has the specified value for the current enumeration type.

Syntax:

public virtual string GetEnumName (object value);

Here, it takes the value whose name is to be retrieved.

Return Value: This method returns the name of the member of the current enumeration type which has the specified value. If no such constant is found then it will return null.

Exceptions:

  • ArgumentException: If the current type is not an enumeration or value is neither of the current type nor does it have the same underlying type as the current type.
  • ArgumentNullException: If value is null.

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.GetEnumName(Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Defining enum ABC
    enum ABC {A, B, C}
  
    // Main Method
    public static void Main()
    {
  
        // Creating 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();
  
            string obj = type.GetEnumName(a);
  
            Console.WriteLine("Name of constant is: " + obj);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e)
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Name of constant is: A

Example 2: For ArgumentException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetEnumName(Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Defining enum ABC
    enum ABC {A, B, C}
  
    // Main Method
    public static void Main()
    {
  
        // Creating 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 = typeof(int);
  
            string obj = type.GetEnumName(a);
  
            Console.WriteLine("Name of constant is: " + obj);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("value is null. ");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
  
        // 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

Example 3: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetEnumName(Object) Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
   // Defining enum ABC
    enum ABC {A, B, C}
  
    // Main Method
    public static void Main()
    {
  
        // Creating 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 = typeof(int);
  
            string obj = type.GetEnumName(null);
  
            Console.WriteLine("Name of constant is " + obj);
        }
  
        // catch ArgumentNullException here
        catch (ArgumentNullException e) 
        {
            Console.WriteLine("value is null. ");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
  
        // 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:

value is null. 
Exception Thrown: System.ArgumentNullException

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.