Open In App

C# | Type.GetEnumUnderlyingType() Method

Last Updated : 15 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Type.GetEnumUnderlyingType() Method is used to return the underlying type of the current enumeration type.

Syntax: 

public virtual Type GetEnumUnderlyingType ();

Return Value: This method returns the underlying type of the current enumeration.

Exception: This method will return the ArgumentException if the current type is not an enumeration or the enumeration type is not valid(due to containing more than one instance field).

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

Example 1: 

C#




// C# program to demonstrate the
// Type.GetEnumUnderlyingType() 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 instance of enum ABC
            ABC a = ABC.A;
 
            // Declaring and initializing
            // object of Type
            Type type = a.GetType();
 
            // Getting underlying type of current enumeration
            // By using GetEnumUnderlyingType() method
            Type obj = type.GetEnumUnderlyingType();
 
            // Display underlying type
            Console.Write("Underlying type is : {0} ", obj);
        }
 
        // 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);
        }
    }
}


Output: 

Underlying type is : System.Int32

 

Example 2: For ArgumentException

C#




// C# program to demonstrate the
// Type.GetEnumUnderlyingType() 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 {
 
            // Declaring and initializing
            // object of Type
            Type type = typeof(int);
 
            // Getting underlying type of current enumeration
            // By using GetEnumUnderlyingType() method
            Type obj = type.GetEnumUnderlyingType();
 
            // Display underlying type
            Console.Write("underlying type is : {0} ", obj);
        }
 
        // 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);
        }
    }
}


Output: 

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

 

Reference:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads