0
0
C Sharp (C#)programming~10 mins

Enum parsing from strings in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enum parsing from strings
Start with string input
Call Enum.Parse method
Check if string matches enum name
Return enum
Use enum value
The program takes a string, tries to convert it to an enum value using Enum.Parse, and either returns the enum or handles errors if the string doesn't match.
Execution Sample
C Sharp (C#)
enum Color { Red, Green, Blue }
string input = "Green";
Color c = (Color)Enum.Parse(typeof(Color), input);
Console.WriteLine(c);
This code converts the string "Green" to the enum Color.Green and prints it.
Execution Table
StepActionInput StringEnum TypeParse ResultOutput
1Start with input string"Green"ColorN/AN/A
2Call Enum.Parse"Green"ColorMatches 'Green'Color.Green
3Cast to Color enumN/AColorColor.GreenColor.Green
4Print enum valueN/AColorColor.GreenGreen
5EndN/AN/AN/AProgram ends
💡 Parsing succeeds because input string matches an enum name exactly.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
input"Green""Green""Green""Green"
cuninitializedN/AColor.GreenColor.Green
Key Moments - 2 Insights
What happens if the input string does not match any enum name?
Enum.Parse throws an exception because it cannot find a matching enum value, as shown by the absence of a successful parse in the execution_table.
Why do we cast the result of Enum.Parse to the enum type?
Enum.Parse returns an object, so we cast it to the specific enum type to use it as that enum, as shown in Step 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of variable 'c' after Step 3?
AColor.Green
Buninitialized
C"Green"
Dnull
💡 Hint
Check the 'Parse Result' and 'Output' columns in Step 3 of the execution_table.
At which step does the program print the enum value?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' and 'Output' columns in the execution_table for when printing happens.
If the input string was "Yellow" which is not in the enum, what would happen?
AEnum.Parse returns null
BEnum.Parse returns default enum value
CEnum.Parse throws an exception
DProgram prints "Yellow"
💡 Hint
Refer to key_moments about what happens when input string does not match any enum name.
Concept Snapshot
Enum parsing from strings in C#:
- Use Enum.Parse(typeof(EnumType), string) to convert string to enum.
- Cast the result to the enum type.
- Throws exception if string doesn't match any enum name.
- Useful to convert user input or text to enum values.
Full Transcript
This example shows how to convert a string like "Green" into an enum value Color.Green using Enum.Parse in C#. The program starts with the input string, calls Enum.Parse with the enum type and string, checks if the string matches an enum name, then casts the result to the enum type. If successful, it prints the enum value. If the string does not match, an exception is thrown. Variables are tracked step-by-step to show how the enum variable 'c' gets assigned. Key moments clarify why casting is needed and what happens on invalid input. The visual quiz tests understanding of variable values at each step and error handling.