Enums let us name groups of related values. Parsing from strings helps convert text into these named values so the program understands them.
Enum parsing from strings in C Sharp (C#)
EnumType variable = (EnumType) Enum.Parse(typeof(EnumType), stringValue); // Or safer: bool success = Enum.TryParse(stringValue, out EnumType variable);
Enum.Parse throws an error if the string is not a valid enum name.
Enum.TryParse returns true or false to show success, avoiding errors.
enum Color { Red, Green, Blue } Color c = (Color) Enum.Parse(typeof(Color), "Green");
enum Color { Red, Green, Blue } if (Enum.TryParse("Blue", out Color c)) { // c is Color.Blue }
enum Color { Red, Green, Blue } // Case insensitive parsing Color c = (Color) Enum.Parse(typeof(Color), "red", true);
This program shows how to parse strings to enum values using both Enum.Parse and Enum.TryParse. It also shows how TryParse handles invalid strings safely.
using System; class Program { enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } static void Main() { string input = "Friday"; // Using Enum.Parse Day day1 = (Day) Enum.Parse(typeof(Day), input); Console.WriteLine($"Parsed with Enum.Parse: {day1}"); // Using Enum.TryParse if (Enum.TryParse("sunday", true, out Day day2)) { Console.WriteLine($"Parsed with Enum.TryParse (case insensitive): {day2}"); } else { Console.WriteLine("Failed to parse day."); } // Try parsing invalid string if (Enum.TryParse("Funday", out Day day3)) { Console.WriteLine($"Parsed: {day3}"); } else { Console.WriteLine("'Funday' is not a valid Day enum value."); } } }
Enum.Parse throws an exception if the string is invalid, so use TryParse to avoid crashes.
TryParse has an overload to ignore case, which is helpful for user input.
Always check the result of TryParse before using the parsed enum value.
Enums group related named values for easy use.
Enum.Parse converts strings to enum but can throw errors if invalid.
Enum.TryParse safely tries to convert strings and tells if it worked.
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
