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
Enum.GetName in C#
The Enum.GetName method in C# returns the string name of a constant in a specified enumeration that has the specified value. This method is useful when you need to convert an enum value back to its string representation for display purposes or logging.
Syntax
Following is the syntax for the Enum.GetName method −
public static string GetName(Type enumType, object value)
Parameters
-
enumType − The enumeration type whose constant's string name you want to retrieve.
-
value − The value of a particular enumerated constant in terms of its underlying type.
Return Value
Returns a string containing the name of the enumerated constant in enumType whose value is value, or null if no such constant is found.
Using Enum.GetName with Different Enum Types
Example
using System;
class Demo {
enum Vehicle {
Car,
Motorbike,
Truck,
Bicycles
};
static void Main() {
// Using GetName to get the string representation of enum value
string res = Enum.GetName(typeof(Vehicle), Vehicle.Motorbike);
// Displaying the result
Console.WriteLine("Enum name: " + res);
// Using with numeric value
string res2 = Enum.GetName(typeof(Vehicle), 2);
Console.WriteLine("Enum name for value 2: " + res2);
}
}
The output of the above code is −
Enum name: Motorbike Enum name for value 2: Truck
Using Enum.GetName with Custom Enum Values
Example
using System;
class Demo {
enum Priority {
Low = 1,
Medium = 5,
High = 10,
Critical = 20
};
static void Main() {
// Get name for explicitly assigned values
Console.WriteLine("Priority 1: " + Enum.GetName(typeof(Priority), 1));
Console.WriteLine("Priority 10: " + Enum.GetName(typeof(Priority), 10));
// Attempt to get name for non-existent value
Console.WriteLine("Priority 3: " + Enum.GetName(typeof(Priority), 3));
// Using enum constant directly
Console.WriteLine("Medium priority: " + Enum.GetName(typeof(Priority), Priority.Medium));
}
}
The output of the above code is −
Priority 1: Low Priority 10: High Priority 3: Medium priority: Medium
Handling Invalid Values
Example
using System;
class Demo {
enum Status {
Active,
Inactive,
Pending
};
static void Main() {
// Valid enum value
string validName = Enum.GetName(typeof(Status), Status.Active);
Console.WriteLine("Valid enum: " + (validName ?? "null"));
// Invalid enum value (returns null)
string invalidName = Enum.GetName(typeof(Status), 99);
Console.WriteLine("Invalid enum: " + (invalidName ?? "null"));
// Check if result is null before using
int testValue = 1;
string result = Enum.GetName(typeof(Status), testValue);
if (result != null) {
Console.WriteLine("Found enum name: " + result);
} else {
Console.WriteLine("No enum found for value: " + testValue);
}
}
}
The output of the above code is −
Valid enum: Active Invalid enum: null Found enum name: Inactive
Conclusion
The Enum.GetName method provides a convenient way to retrieve the string name of an enumeration constant from its value. It returns null if the specified value doesn't correspond to any named constant in the enumeration, making it important to handle null values appropriately.
