An enum (short for enumeration) is a special data type that lets you define a set of named constants, making code easier to read and maintain.
Click to reveal answer
beginner
How do you parse a string to an enum value in C#?
You use Enum.Parse(typeof(EnumType), stringValue) or Enum.TryParse<EnumType>(stringValue, out var result) to convert a string to an enum value.
Click to reveal answer
intermediate
What is the difference between Enum.Parse and Enum.TryParse?
Enum.Parse throws an exception if parsing fails, while Enum.TryParse returns a boolean indicating success or failure without throwing exceptions.
Click to reveal answer
intermediate
How can you make enum parsing case-insensitive?
Use the overload Enum.TryParse<EnumType>(stringValue, ignoreCase: true, out var result) to ignore case differences when parsing.
Click to reveal answer
intermediate
What happens if you try to parse a string that is not a valid enum name?
Using Enum.Parse will throw a System.ArgumentException. Using Enum.TryParse will return false and set the output to the default enum value.
Click to reveal answer
Which method safely parses a string to an enum without throwing exceptions?
AEnum.Parse
BEnum.TryParse
CEnum.ToString
DEnum.GetName
✗ Incorrect
Enum.TryParse returns a boolean indicating success or failure and does not throw exceptions, making it safer for parsing.
How do you parse a string to an enum ignoring case differences?
AEnum.Parse without parameters
BEnum.Parse with ignoreCase parameter set to true
CEnum.TryParse with ignoreCase parameter set to true
DEnum.ToString with ignoreCase
✗ Incorrect
Enum.TryParse<T>(stringValue, ignoreCase: true, out var result) allows case-insensitive parsing.
What exception does Enum.Parse throw if parsing fails?
ASystem.FormatException
BSystem.NullReferenceException
CSystem.InvalidOperationException
DSystem.ArgumentException
✗ Incorrect
Enum.Parse throws System.ArgumentException when the string does not match any enum name.
What is the return type of Enum.TryParse?
Abool
Benum type
Cvoid
Dstring
✗ Incorrect
Enum.TryParse returns a boolean indicating whether parsing succeeded or failed.
Which of these is a valid way to parse a string to an enum value Color?
AEnum.TryParse<Color>("Red", out var color)
BEnum.Parse<Color>("Red")
CColor.Parse("Red")
DEnum.ToString("Red")
✗ Incorrect
Enum.TryParse<Color>(string, out var) is the correct generic method to parse strings to enums safely.
Explain how to convert a string to an enum value in C# and handle invalid input safely.
Think about methods that avoid exceptions and how to check if parsing worked.
You got /5 concepts.
Describe the difference between Enum.Parse and Enum.TryParse when parsing strings to enums.
One method throws exceptions on failure, the other returns a success flag.
You got /4 concepts.
Practice
(1/5)
1. What does Enum.Parse do in C#?
easy
A. Checks if a string is a valid enum name without converting
B. Converts an enum value to a string
C. Creates a new enum type at runtime
D. Converts a string to the corresponding enum value
Solution
Step 1: Understand Enum.Parse purpose
Enum.Parse takes 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 D
Quick Check:
Enum.Parse converts string to enum [OK]
Hint: Enum.Parse converts string to enum value directly [OK]
Common Mistakes:
Thinking Enum.Parse converts enum to string
Confusing Enum.Parse with Enum.TryParse
Believing Enum.Parse checks validity without conversion
2. Which of the following is the correct syntax to parse a string "Monday" to an enum DayOfWeek using Enum.Parse?
easy
A. DayOfWeek day = Enum.Parse("Monday");
B. DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), "Monday");
C. DayOfWeek day = Enum.Parse(DayOfWeek, "Monday");
D. DayOfWeek day = Enum.TryParse("Monday", out DayOfWeek day);
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 B
Quick Check:
Enum.Parse needs typeof and cast [OK]
Hint: Use typeof(Enum) and cast result for Enum.Parse [OK]
Common Mistakes:
Omitting typeof(EnumType)
Not casting the result to enum type
Confusing Enum.Parse with Enum.TryParse syntax
3. What will be the output of this code?
enum Color { Red, Green, Blue }
string input = "Green";
Color c = (Color)Enum.Parse(typeof(Color), input);
Console.WriteLine(c);
medium
A. Green
B. Runtime error
C. Blue
D. Red
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 A
Quick Check:
Enum.Parse("Green") = Color.Green [OK]
Hint: Enum.Parse returns enum matching string name [OK]
Common Mistakes:
Expecting numeric value instead of name
Assuming case-insensitive parsing by default
Thinking it throws error for valid names
4. What is wrong with this code snippet?
enum Status { Active, Inactive }
string input = "Pending";
Status s = (Status)Enum.Parse(typeof(Status), input);
Console.WriteLine(s);
medium
A. It will print "Pending"
B. It will parse to Status.Active by default
C. It will throw a runtime exception
D. It will return null
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 C
Quick Check:
Invalid string causes Enum.Parse exception [OK]
Hint: Enum.Parse throws error if string not in enum [OK]
Common Mistakes:
Expecting default enum value on invalid string
Thinking it returns null instead of error
Confusing with Enum.TryParse behavior
5. You want to safely convert a string "Sunday" to enum DayOfWeek without risking exceptions. Which code snippet correctly does this?
hard
A. bool success = Enum.TryParse("Sunday", true, out DayOfWeek day);
B. DayOfWeek.TryParse("Sunday", out DayOfWeek day);
C. Enum.TryParse(typeof(DayOfWeek), "Sunday", out DayOfWeek day);
D. DayOfWeek day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), "Sunday");
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 A
Quick Check:
Enum.TryParse with out and ignoreCase is safe [OK]
Hint: Use Enum.TryParse with out and ignoreCase for safe parse [OK]
Common Mistakes:
Using Enum.Parse which throws exceptions
Calling TryParse on enum type instead of Enum class
Ignoring the out parameter requirement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.