Enum parsing from strings in C Sharp (C#) - Time & Space Complexity
When we convert text into an enum value, the program checks the text against possible options.
We want to know how the time it takes changes as the number of enum options grows.
Analyze the time complexity of the following code snippet.
enum Color { Red, Green, Blue, Yellow }
string input = "Green";
if (Enum.TryParse(input, out var color))
{
Console.WriteLine($"Parsed color: {color}");
}
else
{
Console.WriteLine("Invalid color");
}
This code tries to convert a string into a matching enum value.
- Primary operation: Checking the input string against each enum name.
- How many times: Once for each enum option until a match is found or all checked.
As the number of enum options grows, the program checks more names one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 string comparisons |
| 100 | Up to 100 string comparisons |
| 1000 | Up to 1000 string comparisons |
Pattern observation: The work grows directly with the number of enum options.
Time Complexity: O(n)
This means the time to parse grows in a straight line as the number of enum values increases.
[X] Wrong: "Parsing an enum from a string is always instant no matter how many options there are."
[OK] Correct: The program checks each enum name one by one, so more options mean more checks and more time.
Understanding how parsing scales helps you explain efficiency and choose better approaches when enums get large.
"What if we used a dictionary to map strings to enum values instead? How would the time complexity change?"
Practice
Enum.Parse do in C#?Solution
Step 1: Understand Enum.Parse purpose
Enum.Parsetakes a string and tries to convert it to the matching enum value.Step 2: Compare options
Only Converts a string to the corresponding enum value describes converting string to enum value. Others describe different actions.Final Answer:
Converts a string to the corresponding enum value -> Option DQuick Check:
Enum.Parse converts string to enum [OK]
- Thinking Enum.Parse converts enum to string
- Confusing Enum.Parse with Enum.TryParse
- Believing Enum.Parse checks validity without conversion
"Monday" to an enum DayOfWeek using Enum.Parse?Solution
Step 1: Recall Enum.Parse syntax
The correct syntax requires specifying the enum type with typeof and casting the result.Step 2: Analyze options
DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), "Monday"); matches the correct syntax: cast the result of Enum.Parse(typeof(EnumType), string).Final Answer:
DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), "Monday"); -> Option BQuick Check:
Enum.Parse needs typeof and cast [OK]
- Omitting typeof(EnumType)
- Not casting the result to enum type
- Confusing Enum.Parse with Enum.TryParse syntax
enum Color { Red, Green, Blue }
string input = "Green";
Color c = (Color)Enum.Parse(typeof(Color), input);
Console.WriteLine(c);Solution
Step 1: Parse string to enum
The string "Green" matches the enum member Color.Green, so Enum.Parse returns that value.Step 2: Print enum value
Console.WriteLine prints the enum member name, which is "Green".Final Answer:
Green -> Option AQuick Check:
Enum.Parse("Green") = Color.Green [OK]
- Expecting numeric value instead of name
- Assuming case-insensitive parsing by default
- Thinking it throws error for valid names
enum Status { Active, Inactive }
string input = "Pending";
Status s = (Status)Enum.Parse(typeof(Status), input);
Console.WriteLine(s);Solution
Step 1: Check if input matches enum members
The string "Pending" is not a member of Status enum, so Enum.Parse cannot convert it.Step 2: Understand Enum.Parse behavior on invalid input
Enum.Parse throws a runtime exception if the string does not match any enum name.Final Answer:
It will throw a runtime exception -> Option CQuick Check:
Invalid string causes Enum.Parse exception [OK]
- Expecting default enum value on invalid string
- Thinking it returns null instead of error
- Confusing with Enum.TryParse behavior
"Sunday" to enum DayOfWeek without risking exceptions. Which code snippet correctly does this?Solution
Step 1: Understand safe parsing with Enum.TryParse
Enum.TryParse returns a bool indicating success and outputs the parsed enum value.Step 2: Check correct method signature
bool success = Enum.TryParse("Sunday", true, out DayOfWeek day); uses Enum.TryParse with ignoreCase=true and out parameter, matching correct safe parsing syntax.Final Answer:
bool success = Enum.TryParse("Sunday", true, out DayOfWeek day); -> Option AQuick Check:
Enum.TryParse with out and ignoreCase is safe [OK]
- Using Enum.Parse which throws exceptions
- Calling TryParse on enum type instead of Enum class
- Ignoring the out parameter requirement
Ready to prove it?
Pass the test and this pattern moves unknown
