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.
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.
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.
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:
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).
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:
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.
- Advantages of C#
- C# vs. Java: What Are Its Main Differences
- Advantages of C# over Python
- First C# Program | Hello World
- Difference between Console.Write and Console.WriteLine in C#
- How do I create a MessageBox in C#?
- C# Comments
- How to reverse a string in C#
- Palindrome in C# with Examples
- Fibonacci Series Program in C# with Examples
- C# Program to Print Number Triangle
- Get Integer Input from User in C# Console Application
- C# StringBuilder | Learn To Use C# StringBuilder Class
- C# HashMap (Dictionary)
- Ternary Operator (? :) in C# with Examples
- How To Sort Datatable in C#
- Struct Vs Class in C# | Differencees and Advantages
- Async And Await In C#
- IEnumerable in C# | Examples
- ref Vs out in C#
- How to remove item from list in C#?
- How to Add Items to a C# List
- C# StreamWriter Examples
- StreamReader in C# |Examples
- C# Map Example
- Static Method In C# | Examples
- How to convert a string to an enum in C#
- How to filter a list in C#?