Type.GetEnumName() Method in C#

The Type.GetEnumName() method in C# returns the name of the constant that has the specified value for the current enumeration type. This method is useful when you need to convert an enum value back to its string representation.

Syntax

Following is the syntax −

public virtual string GetEnumName(object value);

Parameters

  • value − An object whose value is a constant in the current enumeration type.

Return Value

Returns a string containing the name of the enumerated constant that has the specified value, or null if no such constant is found.

Using GetEnumName() with Valid Enum Type

When called on a valid enum type, the method returns the constant name corresponding to the provided value −

using System;

public class Demo {
    enum Vehicle { Car, Bus, Bike }
    
    public static void Main() {
        Vehicle v = Vehicle.Bike;
        Type type = v.GetType();
        string str = type.GetEnumName(v);
        Console.WriteLine("GetEnumName() to return the constant name = " + str);
        
        // Testing with different enum values
        string carName = type.GetEnumName(Vehicle.Car);
        string busName = type.GetEnumName(Vehicle.Bus);
        Console.WriteLine("Car enum name: " + carName);
        Console.WriteLine("Bus enum name: " + busName);
    }
}

The output of the above code is −

GetEnumName() to return the constant name = Bike
Car enum name: Car
Bus enum name: Bus

Handling ArgumentException

When GetEnumName() is called on a non-enum type, it throws an ArgumentException. Here's how to handle this scenario −

using System;

public class Demo {
    enum Vehicle { Car, Bus, Bike }
    
    public static void Main() {
        try {
            Vehicle v = Vehicle.Bike;
            Type type = typeof(string);  // Non-enum type
            string str = type.GetEnumName(v);
            Console.WriteLine("GetEnumName() to return the constant name = " + str);
        }
        catch (ArgumentException e) {
            Console.WriteLine("Not an enum!");
            Console.WriteLine("Exception type: {0}", e.GetType());
        }
    }
}

The output of the above code is −

Not an enum!
Exception type: System.ArgumentException

Using GetEnumName() with Enum Values by Index

You can also use GetEnumName() with integer values that correspond to enum positions −

using System;

public class Demo {
    enum Priority { Low, Medium, High, Critical }
    
    public static void Main() {
        Type enumType = typeof(Priority);
        
        // Get names using integer values
        for (int i = 0; i < 4; i++) {
            string name = enumType.GetEnumName(i);
            Console.WriteLine("Index {0}: {1}", i, name);
        }
        
        // Test with invalid index
        string invalidName = enumType.GetEnumName(10);
        Console.WriteLine("Invalid index result: " + (invalidName ?? "null"));
    }
}

The output of the above code is −

Index 0: Low
Index 1: Medium
Index 2: High
Index 3: Critical
Invalid index result: null

Conclusion

The Type.GetEnumName() method provides a convenient way to retrieve the string name of an enum constant from its value. It returns null for invalid values and throws ArgumentException when called on non-enum types, making proper error handling important in production code.

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

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements