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.GetEnumNames() Method in C#
The Type.GetEnumNames() method in C# is used to return the names of the members of the current enumeration type as an array of strings. This method is particularly useful when you need to dynamically retrieve all the constant names defined in an enum type at runtime.
Syntax
Following is the syntax for the GetEnumNames() method −
public virtual string[] GetEnumNames();
Return Value
This method returns a string array containing the names of the enumeration constants. If the current type is not an enumeration type, it throws an ArgumentException.
Using GetEnumNames() with Enum Types
Example
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[] enumNames = type.GetEnumNames();
Console.WriteLine("Enum type: " + type.Name);
Console.WriteLine("Number of constants: " + enumNames.Length);
Console.WriteLine("Listing constants:");
for (int i = 0; i < enumNames.Length; i++) {
Console.WriteLine("{0}: {1}", i, enumNames[i]);
}
}
catch (ArgumentException e) {
Console.WriteLine("Error: " + e.Message);
}
}
}
The output of the above code is −
Enum type: Vehicle Number of constants: 4 Listing constants: 0: Car 1: Bus 2: Bike 3: Airplane
Using typeof() with GetEnumNames()
Example
using System;
public class Demo {
enum Priority {Low, Medium, High, Critical}
public static void Main() {
try {
Type enumType = typeof(Priority);
string[] names = enumType.GetEnumNames();
Console.WriteLine("Priority enum members:");
foreach (string name in names) {
Console.WriteLine("- " + name);
}
}
catch (ArgumentException e) {
Console.WriteLine("Error: Not an enum type!");
}
}
}
The output of the above code is −
Priority enum members: - Low - Medium - High - Critical
Exception Handling for Non-Enum Types
Example
using System;
public class Demo {
enum Status {Active, Inactive}
public static void Main() {
// This will throw an exception
try {
Type stringType = typeof(string);
string[] names = stringType.GetEnumNames();
Console.WriteLine("This won't execute");
}
catch (ArgumentException e) {
Console.WriteLine("Exception caught: " + e.Message);
}
// This will work correctly
try {
Type enumType = typeof(Status);
string[] names = enumType.GetEnumNames();
Console.WriteLine("Status enum names: " + string.Join(", ", names));
}
catch (ArgumentException e) {
Console.WriteLine("Error: " + e.Message);
}
}
}
The output of the above code is −
Exception caught: Type provided must be an Enum. Status enum names: Active, Inactive
Common Use Cases
-
Dynamic enum processing: When you need to iterate through all enum values without knowing them at compile time.
-
UI generation: Creating dropdown lists or radio buttons from enum values.
-
Configuration validation: Checking if a string value matches any enum constant name.
-
Debugging and logging: Displaying all available options for troubleshooting.
Conclusion
The Type.GetEnumNames() method provides a convenient way to retrieve all constant names from an enumeration type at runtime. It returns a string array containing the names in the order they are declared, making it useful for dynamic enum processing and validation scenarios.
