📖 Scenario: You are building a simple program that reads color names as text and converts them into color codes using an enum.
🎯 Goal: Create an enum called Color with specific colors, then write code to convert a string into the matching Color enum value.
📋 What You'll Learn
Create an enum named Color with values Red, Green, and Blue
Create a string variable named inputColor with the value "Green"
Parse the string inputColor into a Color enum variable named parsedColor
Print the parsedColor variable to the console
💡 Why This Matters
🌍 Real World
Enums are used to represent fixed sets of related values, like days of the week, colors, or states. Parsing strings to enums helps convert user input or data into these fixed categories.
💼 Career
Understanding enums and parsing is important for building clear, maintainable code in many C# applications, including games, business software, and web services.
Progress0 / 4 steps
1
Create the Color enum
Create an enum called Color with the values Red, Green, and Blue.
C Sharp (C#)
Hint
An enum is a special type that holds named constants. Use the enum keyword.
2
Create the input string
Create a string variable called inputColor and set it to the value "Green".
C Sharp (C#)
Hint
Use string inputColor = "Green"; to create the variable.
3
Parse the string to the Color enum
Parse the string inputColor into a Color enum variable called parsedColor using Enum.Parse.
C Sharp (C#)
Hint
Use Enum.Parse(typeof(Color), inputColor) and cast it to Color.
4
Print the parsed enum value
Print the parsedColor variable to the console using Console.WriteLine.
C Sharp (C#)
Hint
Use Console.WriteLine(parsedColor); to show the result.
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.