Convert An Enum To String In C#

Sameer Saini March 02, 2022
Convert An Enum To String In C#

In this blog post, we will convert an Enum to string in C#.

An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type.

We can convert an enum to a string using the .ToString() method on the Enum.

Let's see this in an example:

public enum Season { Spring, Summer, Autumn, Winter }

Console.WriteLine(Season.Summer.ToString()); // Result: Summer Console.WriteLine(Season.Spring.ToString()); // Result: Spring

 

In the above code example, we can see that we can convert any Enum value to a string value in C# and then once it has been converted to a string, we can use any methods that are available to strings on that variable.

 

Why Do We Need To Convert An Enum To String

In some scenarios, we need to convert an Enum value to string just because we want to store it in the database.

In some situations, we want to display the value of an enum on the UI or our application, so we would need a conversion from an Enum type to a string type.

Example: The months of an year can be displayed as a dropdown by storing the months as an Enum and then iterating on the Enum to display on the UI by converting all of the Enum values to string values.