0
0
CsharpHow-ToBeginner · 2 min read

C# How to Convert String to Enum Easily

Use Enum.Parse(typeof(EnumType), stringValue) or safer Enum.TryParse<EnumType>(stringValue, out var result) to convert a string to an enum in C#.
📋

Examples

Input"Monday"
OutputDayOfWeek.Monday
Input"Friday"
OutputDayOfWeek.Friday
Input"Funday"
OutputConversion fails or returns false
🧠

How to Think About It

To convert a string to an enum, think of matching the string exactly to one of the enum names. You can try to parse the string safely to avoid errors if the string doesn't match any enum value.
📐

Algorithm

1
Get the input string to convert.
2
Use a parsing method to check if the string matches an enum name.
3
If it matches, return the corresponding enum value.
4
If it doesn't match, handle the failure safely.
💻

Code

csharp
using System;

enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }

class Program {
    static void Main() {
        string input = "Monday";
        if (Enum.TryParse<DayOfWeek>(input, out DayOfWeek day)) {
            Console.WriteLine(day);
        } else {
            Console.WriteLine("Invalid enum string");
        }
    }
}
Output
Monday
🔍

Dry Run

Let's trace converting the string "Monday" to the enum DayOfWeek.

1

Input string

input = "Monday"

2

TryParse call

Enum.TryParse("Monday", out day) returns true and day = DayOfWeek.Monday

3

Output result

Prints "Monday"

Input StringTryParse ResultEnum Value
MondaytrueDayOfWeek.Monday
💡

Why This Works

Step 1: Enum.Parse vs Enum.TryParse

Enum.Parse converts string but throws exception if invalid; Enum.TryParse returns true/false safely.

Step 2: Matching string to enum

The string must exactly match an enum name (case-insensitive optional) to convert successfully.

Step 3: Output usage

After conversion, you can use the enum value in your program as a typed constant.

🔄

Alternative Approaches

Enum.Parse with exception handling
csharp
try {
    DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), "Monday");
    Console.WriteLine(day);
} catch (ArgumentException) {
    Console.WriteLine("Invalid enum string");
}
Throws exception on invalid input, so you must catch it.
Case-insensitive TryParse
csharp
if (Enum.TryParse<DayOfWeek>("monday", true, out DayOfWeek day)) {
    Console.WriteLine(day);
} else {
    Console.WriteLine("Invalid enum string");
}
Allows case-insensitive matching by passing true as second argument.

Complexity: O(1) time, O(1) space

Time Complexity

Parsing a string to enum is a constant time operation because it checks a fixed set of enum names.

Space Complexity

No extra memory is allocated besides the output enum variable, so space is constant.

Which Approach is Fastest?

Enum.TryParse is efficient and safe; Enum.Parse is similar but requires exception handling.

ApproachTimeSpaceBest For
Enum.TryParseO(1)O(1)Safe parsing without exceptions
Enum.Parse with try-catchO(1)O(1)Simple code but needs exception handling
Direct cast (invalid)N/AN/ADoes not compile
💡
Use Enum.TryParse to safely convert strings to enums without exceptions.
⚠️
Trying to cast string directly to enum without parsing causes compile errors.