C# | Type.GetEnumUnderlyingType() Method
Last Updated :
15 Jul, 2021
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#
using System;
using System.Globalization;
using System.Reflection;
class GFG {
enum ABC { A,
B,
C,
D,
E,
F }
public static void Main()
{
try {
ABC a = ABC.A;
Type type = a.GetType();
Type obj = type.GetEnumUnderlyingType();
Console.Write( "Underlying type is : {0} " , obj);
}
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#
using System;
using System.Globalization;
using System.Reflection;
class GFG {
enum ABC { A,
B,
C,
D,
E,
F }
public static void Main()
{
try {
Type type = typeof ( int );
Type obj = type.GetEnumUnderlyingType();
Console.Write( "underlying type is : {0} " , obj);
}
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:
Share your thoughts in the comments
Please Login to comment...