Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Type.GetEnumUnderlyingType() Method in C#
The Type.GetEnumUnderlyingType() method in C# returns the underlying data type of an enumeration. By default, enums are based on int, but they can also be based on other integral types like byte, short, long, etc.
Syntax
Following is the syntax −
public virtual Type GetEnumUnderlyingType();
Return Value
Returns a Type object representing the underlying type of the enumeration. Throws ArgumentException if the current type is not an enumeration.
Using GetEnumUnderlyingType() with Default Enum
Let us see an example that demonstrates getting the underlying type of a default enum −
using System;
public class Demo {
enum Vehicle { Car, Bus, Bike, Airplane }
public static void Main() {
try {
Vehicle v = Vehicle.Bike;
Type type = v.GetType();
string[] str = type.GetEnumNames();
Console.WriteLine("Enum names: " + string.Join(", ", str));
Type underlyingType = type.GetEnumUnderlyingType();
Console.WriteLine("Enum underlying type: " + underlyingType);
Console.WriteLine("Listing constants:");
for (int i = 0; i < str.Length; i++)
Console.Write("{0} ", str[i]);
}
catch (ArgumentException e) {
Console.WriteLine("Not an enum!");
Console.WriteLine("Error: " + e.Message);
}
}
}
The output of the above code is −
Enum names: Car, Bus, Bike, Airplane Enum underlying type: System.Int32 Listing constants: Car Bus Bike Airplane
Using GetEnumUnderlyingType() with Custom Underlying Types
Enums can have different underlying types specified explicitly −
using System;
public class Demo {
enum StatusByte : byte { Active = 1, Inactive = 0, Pending = 2 }
enum StatusLong : long { Processing = 100L, Complete = 200L, Failed = 300L }
public static void Main() {
Type byteEnumType = typeof(StatusByte);
Type longEnumType = typeof(StatusLong);
Console.WriteLine("StatusByte underlying type: " + byteEnumType.GetEnumUnderlyingType());
Console.WriteLine("StatusLong underlying type: " + longEnumType.GetEnumUnderlyingType());
// Show enum values with their underlying values
Console.WriteLine("\nStatusByte values:");
foreach (StatusByte status in Enum.GetValues(typeof(StatusByte))) {
Console.WriteLine("{0} = {1}", status, (byte)status);
}
}
}
The output of the above code is −
StatusByte underlying type: System.Byte StatusLong underlying type: System.Int64 StatusByte values: Inactive = 0 Active = 1 Pending = 2
Handling Non-Enum Types
The method throws an exception when called on non-enum types −
using System;
public class Demo {
public static void Main() {
try {
Type intType = typeof(int);
Type underlyingType = intType.GetEnumUnderlyingType();
Console.WriteLine("This won't execute");
}
catch (ArgumentException e) {
Console.WriteLine("Error: " + e.Message);
Console.WriteLine("Type 'int' is not an enumeration type.");
}
}
}
The output of the above code is −
Error: Type must be a enum. Type 'int' is not an enumeration type.
Common Enum Underlying Types
| Underlying Type | Size | Range |
|---|---|---|
| byte | 1 byte | 0 to 255 |
| short | 2 bytes | -32,768 to 32,767 |
| int (default) | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| long | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Conclusion
The GetEnumUnderlyingType() method is useful for reflection scenarios where you need to determine the data type that backs an enumeration. It returns System.Int32 by default, but can return other integral types when explicitly specified in the enum declaration.
