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
C# Enum Parse Method
The Enum.Parse method in C# converts the string representation of an enum constant name or its numeric value into an equivalent enumerated object. This method is essential when you need to convert string data back into strongly-typed enum values.
Syntax
Following are the main syntax forms for Enum.Parse −
// Basic syntax public static object Parse(Type enumType, string value) // With ignore case option public static object Parse(Type enumType, string value, bool ignoreCase)
Parameters
enumType − The type of the enumeration to parse to
value − A string containing the name or value to convert
ignoreCase − Boolean indicating whether to ignore case when parsing
Return Value
Returns an object of type enumType whose value is represented by value. The result must be cast to the appropriate enum type.
Using Enum.Parse with String Names
The most common usage is parsing string names back to enum values −
using System;
public class Demo {
enum Vehicle { Car, Bus, Truck, Motorbike }
public static void Main() {
// Parse string names to enum values
Vehicle car = (Vehicle)Enum.Parse(typeof(Vehicle), "Car");
Vehicle bus = (Vehicle)Enum.Parse(typeof(Vehicle), "Bus");
Console.WriteLine("Parsed enum values:");
Console.WriteLine("Car = " + (int)car);
Console.WriteLine("Bus = " + (int)bus);
// Using the parsed values
Console.WriteLine("Vehicle type: " + car);
}
}
The output of the above code is −
Parsed enum values: Car = 0 Bus = 1 Vehicle type: Car
Using Enum.Parse with Case Insensitive Parsing
The third parameter allows case-insensitive parsing, which is useful when dealing with user input −
using System;
public class Demo {
enum Status { Active, Inactive, Pending }
public static void Main() {
// Case sensitive parsing (default)
Status status1 = (Status)Enum.Parse(typeof(Status), "Active");
// Case insensitive parsing
Status status2 = (Status)Enum.Parse(typeof(Status), "active", true);
Status status3 = (Status)Enum.Parse(typeof(Status), "PENDING", true);
Console.WriteLine("Case sensitive: " + status1);
Console.WriteLine("Case insensitive 'active': " + status2);
Console.WriteLine("Case insensitive 'PENDING': " + status3);
}
}
The output of the above code is −
Case sensitive: Active Case insensitive 'active': Active Case insensitive 'PENDING': Pending
Using Enum.Parse with Numeric Values
You can also parse numeric string values to enum constants −
using System;
public class Demo {
enum Priority { Low = 1, Medium = 5, High = 10 }
public static void Main() {
// Parse numeric string values
Priority low = (Priority)Enum.Parse(typeof(Priority), "1");
Priority medium = (Priority)Enum.Parse(typeof(Priority), "5");
Priority high = (Priority)Enum.Parse(typeof(Priority), "10");
Console.WriteLine("Numeric parsing results:");
Console.WriteLine("1 = " + low);
Console.WriteLine("5 = " + medium);
Console.WriteLine("10 = " + high);
}
}
The output of the above code is −
Numeric parsing results: 1 = Low 5 = Medium 10 = High
Error Handling with Enum.Parse
When the string value cannot be parsed, Enum.Parse throws an ArgumentException. It's important to handle this properly −
using System;
public class Demo {
enum Color { Red, Green, Blue }
public static void Main() {
string[] testValues = { "Red", "Yellow", "green" };
foreach (string value in testValues) {
try {
Color color = (Color)Enum.Parse(typeof(Color), value);
Console.WriteLine("Successfully parsed: " + value + " = " + color);
}
catch (ArgumentException) {
Console.WriteLine("Failed to parse: " + value);
}
}
}
}
The output of the above code is −
Successfully parsed: Red = Red Failed to parse: Yellow Failed to parse: green
Conclusion
The Enum.Parse method provides a powerful way to convert string representations back to enum values in C#. Remember to cast the result to your specific enum type and handle potential ArgumentException cases when the string cannot be parsed to prevent runtime errors.
