Here you will learn how to convert the string representation of enum member to enum in C#.
Use the following methods to convert from the string to enum:
Method | Description |
---|---|
Enum.TryParse()
|
Converts the string representation of one or more enum member names or numeric values to an equivalent enum objects. It returns true if the conversion succeeded and false if failed.
It is recommended to use the Enum.TryParse() over Enum.Parse() method.
|
Enum.Parse()
|
Converts the string representation of one or more enum member name or numeric value to an equivalent enum object. |
Enum.TryParse() Method
The Enum.TryParse()
method converts the string representation of enum member name or numeric value to an equivalent enum object.
bool TryParse(string value, out object result)
bool TryParse(string value, bool ignoreCase, out object result)
bool TryParse<TEnum>(string value, out TEnum result)
bool TryParse<TEnum>(string value, bool ignoreCase, out TEnum result)
The Enum.TryParse()
method returns a boolean to indicate whether the specified string is converted to enum or not. Returns true
if the conversion succeeded; otherwise, returns false
.
The result would be stored in an out parameter result
if the conversion succeded.
It never throws exceptions if the conversion fails; instead, it returns false
.
It is best practice to use the TryParse()
method that does not raise exceptions.
The following example shows the conversion of string to enum using TryParse<TEnum>()
method in .NET 4.x and .NET 5:
enum Week
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
public static void Main()
{
var day1 = "Monday";
var day2 = "MONDAY";
var day3 = "SomeOtherDay";
Week week1, week2, week3;
if (Enum.TryParse<Week>(day1, out week1))
Console.WriteLine("{0} converted to {1}", day1, week1);
if (Enum.TryParse<Week>(day2, true, out week2)) // ignore cases
Console.WriteLine("{0} converted to {1}", day2, week2);
if (Enum.TryParse<Week>(day3, true, out week3)) // ignore cases
Console.WriteLine(Console.WriteLine("{0} converted to {1}", day3, week3););
else
Console.WriteLine("Conversion Faild for {0}", day3);
}
MONDAY converted to Monday
Conversion Faild for SomeOtherDay
In the above example, Enum.TryParse()
converts the three different strings to enum members. A string MONDAY
converted to enum by passing true
to ignore cases of the string value.
However, Enum.TryParse<Week>(day3, true, out week3)
returns false
because it cannot find a member named "SomeOtherDay". So, instead of throwing an exception, returns false
boolean value.
Enum.Parse() Method
The Enum.Parse()
method parse the specified string to enum member. However, it throws an exception if the specified string does not match with any enum member name.
The Enum.Parse()
method has the following overloads in .NET Framework 4.x and 5:
Parse(Type enumType, string value)
Parse(Type enumType, string value, bool ignoreCase)
//.NET 5.0 includes the following overloads:
Parse<TEnum>(string value, bool ignoreCase)
Parse<TEnum>(string value)
The following example demonstrates the conversion of string to enum using the Parse()
method in .NET 4.x:
enum Week
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
public static void Main()
{
string day1 = "Monday";
string day2 = "MONDAY";
string day3 = "SomeOtherDay";
Week weekDay1 = (Week) Enum.Parse(typeof(Week), day1);
Week weekDay2 = (Week) Enum.Parse(typeof(Week), day2, true); // ignore case
// the following will throw an exception
//Week weekDay3 = (Week) Enum.Parse(typeof(Week), day3);
Console.WriteLine(weekDay1);
Console.WriteLine(weekDay2);
}
In the above example, the Enum.Parse()
method converts the string value day1
to enumeration type and returns the result as an enumeration object.
The boolean parameter specifies if the operation is case-insensitive. Pass true
to ignore case to compare string and enum member values.
Use the generic Enum.Parse<TEnum>()
method in .NET 5.x:
enum Week
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
public static void Main()
{
var day1 = "Monday";
var day2 = "MONDAY";
var day3 = "SomeOtherDay";
Week weekDay1 = Enum.Parse<Week>(day1);
Week weekDay2 = Enum.Parse<Week>(day2, true); // ignore cases
// the following will throw an exception
//Week weekDay3 = (Week) Enum.Parse<Week>(day3);
Console.WriteLine(weekDay1);
Console.WriteLine(weekDay2);
}
Note that the string value must match with any member names; otherwise it will throw an exception.
Use the Enum.IsDefined()
method to check if a given string name or integer value is defined in a specified enumeration.
if(Enum.IsDefined(typeof(Week), day3){
Week week3 = Enum.Parse<Week>(day3);
}
else{
Console.WriteLine("Wrong Week Day");
}
Thus, the conversion of String to Enum can be implemented using the Enum.Parse
(
)
and Enum.TryParse
()
method.
- How to validate email in C#?
- How to get the sizeof a datatype in C#?
- Difference between String and StringBuilder in C#
- Static vs Singleton in C#
- Difference between == and Equals() Method in C#
- Asynchronous programming with async, await, Task in C#
- How to loop through an enum in C#?
- Generate Random Numbers in C#
- Difference between Two Dates in C#
- Convert int to enum in C#
- BigInteger Data Type in C#
- Convert an Object to JSON in C#
- Convert JSON String to Object in C#
- DateTime Formats in C#
- How to convert date object to string in C#?
- Compare strings in C#
- How to count elements in C# array?
- Difference between String and string in C#.
- How to get a comma separated string from an array in C#?
- Boxing and Unboxing in C#
- How to convert string to int in C#?