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.GetEnumValues() Method in C#
The Type.GetEnumValues() method in C# returns an array containing the values of all constants in an enumeration type. This method is useful when you need to iterate through all possible values of an enum or perform operations on the entire set of enum values.
Syntax
Following is the syntax for the GetEnumValues() method −
public virtual Array GetEnumValues();
Return Value
Returns an Array that contains the values of the constants in the current enumeration type. The elements of the array are sorted by the binary values of the enumeration constants.
Using GetEnumValues() with Valid Enum Type
The following example demonstrates how to use GetEnumValues() with a proper enumeration type −
using System;
public class Demo {
enum Vehicle { Car, Bus, Bike, Airplane }
public static void Main() {
Type type = typeof(Vehicle);
Array enumValues = type.GetEnumValues();
Console.WriteLine("Enum Values:");
foreach (Vehicle vehicle in enumValues) {
Console.WriteLine($"{vehicle} = {(int)vehicle}");
}
Console.WriteLine("\nUsing GetEnumNames() for comparison:");
string[] enumNames = type.GetEnumNames();
for (int i = 0; i < enumNames.Length; i++) {
Console.WriteLine($"Name: {enumNames[i]}, Value: {enumValues.GetValue(i)}");
}
}
}
The output of the above code is −
Enum Values: Car = 0 Bus = 1 Bike = 2 Airplane = 3 Using GetEnumNames() for comparison: Name: Car, Value: Car Name: Bus, Value: Bus Name: Bike, Value: Bike Name: Airplane, Value: Airplane
Exception Handling with Non-Enum Types
When GetEnumValues() is called on a type that is not an enumeration, it throws an ArgumentException −
using System;
public class Demo {
public static void Main() {
try {
Type type = typeof(int);
Array enumValues = type.GetEnumValues();
Console.WriteLine("This line won't execute");
}
catch (ArgumentException e) {
Console.WriteLine("Exception caught: Not an enum type!");
Console.WriteLine($"Exception Type: {e.GetType().Name}");
Console.WriteLine($"Message: {e.Message}");
}
}
}
The output of the above code is −
Exception caught: Not an enum type! Exception Type: ArgumentException Message: Type provided must be an Enum.
Using GetEnumValues() with Custom Enum Values
This example shows how GetEnumValues() works with enums that have custom numeric values −
using System;
public class Demo {
enum Priority { Low = 1, Medium = 5, High = 10, Critical = 20 }
public static void Main() {
Type type = typeof(Priority);
Array enumValues = type.GetEnumValues();
Console.WriteLine("Priority enum values (sorted by binary value):");
foreach (Priority priority in enumValues) {
Console.WriteLine($"{priority} = {(int)priority}");
}
Console.WriteLine($"\nTotal enum values: {enumValues.Length}");
Console.WriteLine($"Underlying type: {type.GetEnumUnderlyingType()}");
}
}
The output of the above code is −
Priority enum values (sorted by binary value): Low = 1 Medium = 5 High = 10 Critical = 20 Total enum values: 4 Underlying type: System.Int32
Common Use Cases
-
Populating dropdown lists with all enum values in user interfaces.
-
Validation to check if a given value exists in an enumeration.
-
Iteration through all possible enum values for processing or display.
-
Reflection-based operations when working with enums dynamically at runtime.
Conclusion
The Type.GetEnumValues() method provides a convenient way to retrieve all values from an enumeration type at runtime. It returns an array sorted by the binary values of the enum constants and throws an ArgumentException if called on non-enum types.
