Recall & Review
beginner
What is an enum in C#?
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?
✗ 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?
✗ Incorrect
Enum.TryParse<T>(stringValue, ignoreCase: true, out var result) allows case-insensitive parsing.
What exception does
Enum.Parse throw if parsing fails?✗ Incorrect
Enum.Parse throws System.ArgumentException when the string does not match any enum name.
What is the return type of
Enum.TryParse?✗ 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?✗ 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.