C# How to Convert Enum to String Easily
In C#, convert an enum to string by calling
ToString() on the enum value, like myEnumValue.ToString().Examples
InputDayOfWeek.Monday
Output"Monday"
InputConsoleColor.Red
Output"Red"
Input(MyEnum)100
Output"100"
How to Think About It
To convert an enum to a string, think of the enum value as a label or name. Calling
ToString() on it returns the name of the enum member as text. If the value doesn't match a named member, it returns the numeric value as a string.Algorithm
1
Get the enum value you want to convert.2
Call the <code>ToString()</code> method on this enum value.3
Return the resulting string.Code
csharp
using System;
enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
class Program {
static void Main() {
DayOfWeek today = DayOfWeek.Monday;
string dayName = today.ToString();
Console.WriteLine(dayName);
}
}Output
Monday
Dry Run
Let's trace converting DayOfWeek.Monday to string.
1
Assign enum value
today = DayOfWeek.Monday (numeric value 1)
2
Call ToString()
today.ToString() returns "Monday"
3
Print result
Console prints "Monday"
| Step | Enum Value | ToString() Result |
|---|---|---|
| 1 | DayOfWeek.Monday (1) | "Monday" |
Why This Works
Step 1: Enum values have names
Each enum member has a name and an underlying numeric value.
Step 2: ToString() returns the name
Calling ToString() on an enum returns its name as a string.
Step 3: Handles unknown values
If the enum value has no matching name, ToString() returns the numeric value as a string.
Alternative Approaches
Using Enum.GetName()
csharp
using System;
enum Color { Red, Green, Blue }
class Program {
static void Main() {
Color c = Color.Green;
string name = Enum.GetName(typeof(Color), c);
Console.WriteLine(name);
}
}This method returns null if the value is not defined in the enum.
Using string interpolation
csharp
using System;
enum Status { Active, Inactive }
class Program {
static void Main() {
Status s = Status.Active;
string result = $"{s}";
Console.WriteLine(result);
}
}This is a shorthand that internally calls ToString(), useful for quick conversions.
Complexity: O(1) time, O(1) space
Time Complexity
Converting an enum to string is a simple lookup and string return, so it runs in constant time.
Space Complexity
Only a small string object is created to hold the name, so space usage is constant.
Which Approach is Fastest?
Calling ToString() is the fastest and simplest; Enum.GetName() adds a small overhead but can return null for undefined values.
| Approach | Time | Space | Best For |
|---|---|---|---|
| ToString() | O(1) | O(1) | Simple and direct conversion |
| Enum.GetName() | O(1) | O(1) | When you want null for undefined values |
| String Interpolation | O(1) | O(1) | Quick inline conversion |
Use
ToString() directly on the enum value for the simplest conversion.Trying to cast an enum to string directly without calling
ToString() will cause a compile error.