C# Enum ToString: Convert Enum to String

In C#, an enumeration type, also known as an enum, is a set of named constants that represent a specific group of values. Converting an enum to a string involves displaying the textual representation of the enum's value. Following are several ways to convert an enum to a string in C#:

Using the ToString() method

The ToString() method is a built-in method in C# that returns a string that represents the current enum value.

using System; public class MyProgram { enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } public static void Main(string[] args) { DayOfWeek day = DayOfWeek.Sunday; string dayAsString = day.ToString(); Console.WriteLine(dayAsString + " "+ dayAsString.GetType()); Console.WriteLine(day + " " + day.GetType()); } }
//Output: Sunday System.String Sunday DayOfWeek

In the above example, the variable dayAsString will contain the string "Sunday".

Using the Enum.GetName() method

The Enum.GetName() method is a built-in method in C# that returns the name of the constant in the enum that has the specified value.

DayOfWeek day = DayOfWeek.Sunday; string dayAsString = Enum.GetName(typeof(DayOfWeek), day); Console.WriteLine(dayAsString + " "+ dayAsString.GetType()); Console.WriteLine(day + " " + day.GetType());
//Output: Sunday System.String Sunday DayOfWeek

In the above example, the variable dayAsString will also contain the string "Sunday".

Using Enum.GetValues() and foreach loop

The Enum.GetValues() method is a built-in method in C# that returns an array of the values in the enum. You can use this method along with a foreach loop to iterate through the values and convert each one to a string.

using System; public class MyProgram { enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } public static void Main(string[] args) { foreach (DayOfWeek day in Enum.GetValues(typeof(DayOfWeek))) { string dayAsString = day.ToString(); Console.WriteLine(dayAsString); } } }
//Output: Sunday Monday Tuesday Wednesday Thursday Friday Saturday

In the above example, the foreach loop will iterate through each value in the DayOfWeek enum and convert it to a string using the ToString() method.

Using enum.parse()

Enum.Parse() is a built-in method in C# that allows you to convert a string representation of an enum to the corresponding enum value. Following an example of how to use Enum.Parse() to convert an enum to a string:

using System; public class MyProgram { enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } public static void Main(string[] args) { string dayString = "Monday"; DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), dayString); Console.WriteLine(day); } }
//Output: Monday

In the above example, there is a string representation of an enum value ("Monday") that you want to convert to the corresponding DayOfWeek enum value. Use Enum.Parse() to accomplish this, passing in the type of the enum you want to convert to (typeof(DayOfWeek)) and the string representation of the enum value you want to convert (dayString).

The Enum.Parse() method returns an object of type System.Object, so you need to cast the result to the appropriate enum type ((DayOfWeek) in this case) in order to assign it to a variable of that type (day in this case).


How to convert string to enum in C#

After this conversion, the day variable will contain the DayOfWeek enum value corresponding to the original string representation "Monday".

Note that if the input string does not match any of the named constants in the enum, Enum.Parse() will throw an ArgumentException. To handle this scenario, you can use Enum.TryParse() instead, which will return a boolean indicating whether the conversion was successful, rather than throwing an exception.

Using enum.tryparse()

Enum.TryParse() is another built-in method in C# that allows you to convert a string representation of an enum to the corresponding enum value. Following is an example of how to use Enum.TryParse() to convert an enum to a string:

using System; public class MyProgram { enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } public static void Main(string[] args) { string dayString = "Monday"; DayOfWeek day; if (Enum.TryParse(dayString, out day)) { Console.WriteLine("Converted '{0}' to {1}.", dayString, day.ToString()); } else { Console.WriteLine("Unable to convert '{0}' to a DayOfWeek value.", dayString); } } }
//Output: Converted 'Monday' to Monday.

In the above example, there is a string representation of an enum value ("Monday") that you want to convert to the corresponding DayOfWeek enum value. Use Enum.TryParse() to accomplish this, passing in the string representation of the enum value you want to convert (dayString) and an output parameter of the appropriate enum type (day in this case).

If the input string matches one of the named constants in the DayOfWeek enum, Enum.TryParse() will set the day variable to the corresponding enum value and return true. Otherwise, it will return false.

In the above example, check the return value of Enum.TryParse() to determine whether the conversion was successful. If it was, you can access the converted DayOfWeek value through the day variable and display its string representation using the ToString() method. If the conversion was not successful, display an error message.

Using Enum.TryParse() instead of Enum.Parse() can be useful in scenarios where you want to handle invalid input, without throwing an exception.

These are some of the ways to convert an enum to a string in C#. The method you choose will depend on your specific needs and the structure of your code.