Javatpoint Logo
Javatpoint Logo

Type.GetEnumUnderlyingType() Method in C#

In this article, we will discuss the Type.GetEnumUnderlying() method in C# with its syntax and examples.

What is the Type.GetEnumUnderlying() Method?

The Type.GetEnumUnderlying() method is used to get the integral type of an enumeration. This method is present in the System.Type class in the C# programming language. It helps when working with an enumeration's structure. This method reveals the integral data type that stores the values of the enum's members. This underlying type is also important for serialization and numeric operations.


It has the following syntax:

In the above syntax, the arguments are of Type enum. The enumType is the Type object, which represents the enumeration type of the enum.

This method returns the underlying integral datatype of the enum. The type may be int, byte, sbyte, short, ushort, long, ulong, uint etc. This method will return the null value if the type is not an enum.

Some key points related to the method:

Some main key points of the Type.GetEnumUnderlying() method are as follows:

Enumeration values: The enum members are stored as values of the underlying type.

Common Underlying Types: The int is the default underlying type.

Type checking: This method is used to check the type of the enum before performing the enum-specific operations.

The main use cases are:

Serialization and deserialization of enum values. This method is useful for performing bitwise and arithmetic operations on enum values and for comparing enum values to the numeric values of the underlying type.


Let us take a C# program to demonstrate the Type.GetEnumUnderlyingType() method.


Type.GetEnumUnderlyingType() Method in C#


This program will illustrate the importance of Type.GetEnumUnderlyingType() method. First, an enumeration is declared with the short datatype. The enum values are 'Basic', 'Standard', and 'Premium', which represent the 16-bit integer. In the main method, the enum id is declared the current level, representing the enum's Standard value. After that, the GetEnumUnderlying method is used to check the type of the enum. Next, print whether the enum type is of short type or not. This program will exit after the execution of the printing of the result of the check.

Example 2:

Let us take another example to illustrate the Type.GetEnumUnderlyingType() Method in C#.


Type.GetEnumUnderlyingType() Method in C#


This program is used to demonstrate the Enum.GetUnderlyingType(). First, an enum is named 'WorkHours' with explicit values containing the days in a week. In the main method, we store the enum's values in a string array using the function GetRnumNames(). After that, the underlying type of the enum is displayed using Enum.GetUnderlyingType() method. This program will iterate through the enum keys, parse them into enum type, and access their corresponding value. This program uses the error handling method by using the try and catch blocks to handle the errors.


In conclusion, the Type.GetEnumUnderlyingtype() method plays a crucial method in returning the integral datatype used to store the values of an enumeration. This method belongs to the System.Type class. This method has many applications in daily life. Some are serialization, deserialization, bitwise operations, arithmetic operations, type checking, etc. Before using this method, the developers must evaluate the trade-offs between performance and type safety, readability and maintainability. This method will be helpful while working with the enums so that the developers can safely use enums and easily find the integral type of the enum so that the developer will avoid errors. This method will enable a variety of operations and techniques.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA