C# Program to get the type of the specified Enumeration

Use the GetType() method to get the type of the specified enumeration in C#. This method returns a Type object that represents the actual enumeration type, which is useful for reflection, debugging, and type checking operations.

When working with enumeration values, you can retrieve both the enumeration type and its underlying type using built-in methods provided by the Enum class and Type class.

Syntax

Following is the syntax for getting the type of an enumeration −

Type enumType = enumValue.GetType();

To get the underlying type of an enumeration −

Type underlyingType = Enum.GetUnderlyingType(enumType);

Using GetType() with Multiple Enumerations

You can work with different enumeration types and retrieve their type information dynamically −

using System;

public class Demo {
   public static void Main() {
      Enum[] values = { ConsoleColor.Blue, DayOfWeek.Sunday};
      Console.WriteLine("{0,-5} {1, 10} {2,10}<br>", "Member", "Enumeration", "UnderlyingType");
      foreach (var val in values)
         Info(val);
   }
   
   static void Info(Enum val) {
      Type enumType = val.GetType();
      Type underlyingType = Enum.GetUnderlyingType(enumType);
      Console.WriteLine("{0, -5} {1, 10} {2,10}", val, enumType.Name, underlyingType.Name);
   }
}

The output of the above code is −

Member Enumeration UnderlyingType

Blue ConsoleColor Int32
Sunday DayOfWeek Int32

Using GetType() with Custom Enumeration

You can also create custom enumerations and retrieve their type information −

using System;

public enum Priority : byte {
   Low = 1,
   Medium = 2,
   High = 3
}

public enum Status : short {
   Pending = 100,
   InProgress = 200,
   Completed = 300
}

public class Program {
   public static void Main() {
      Priority taskPriority = Priority.High;
      Status taskStatus = Status.InProgress;
      
      Console.WriteLine("Priority Type: " + taskPriority.GetType().Name);
      Console.WriteLine("Priority Value: " + taskPriority);
      Console.WriteLine("Priority Underlying Type: " + Enum.GetUnderlyingType(taskPriority.GetType()).Name);
      Console.WriteLine();
      
      Console.WriteLine("Status Type: " + taskStatus.GetType().Name);
      Console.WriteLine("Status Value: " + taskStatus);
      Console.WriteLine("Status Underlying Type: " + Enum.GetUnderlyingType(taskStatus.GetType()).Name);
   }
}

The output of the above code is −

Priority Type: Priority
Priority Value: High
Priority Underlying Type: Byte

Status Type: Status
Status Value: InProgress
Status Underlying Type: Int16

Comparison of Type Information Methods

Method Returns Purpose
GetType() Type of the enumeration Gets the actual enumeration type (e.g., ConsoleColor)
Enum.GetUnderlyingType() Underlying data type Gets the base type used to store enum values (e.g., Int32)
typeof() Type at compile time Gets the type without needing an instance

Conclusion

The GetType() method provides runtime type information for enumeration values, returning the specific enumeration type. Combined with Enum.GetUnderlyingType(), you can retrieve both the enumeration type and its underlying storage type, which is valuable for reflection and type analysis scenarios.

Updated on: 2026-03-17T07:04:35+05:30

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements