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

Enum parsing from strings in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to parse the string "Red" into the Color enum.

C Sharp (C#)
Color color = (Color) Enum.Parse(typeof(Color), [1]);
Drag options to blanks, or click blank then click option'
A"Red"
BRed
Ccolor
Dtypeof(Color)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the enum name without quotes causes a compile error.
Passing the enum type instead of the string name.
2fill in blank
medium

Complete the code to safely parse the string "Blue" into the Color enum using TryParse.

C Sharp (C#)
bool success = Enum.TryParse<Color>([1], out Color color);
Drag options to blanks, or click blank then click option'
Acolor
Btypeof(Color)
C"Blue"
DBlue
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the enum variable instead of the string.
Passing the enum type instead of the string.
3fill in blank
hard

Fix the error in parsing the string "Green" to the Color enum using Enum.Parse.

C Sharp (C#)
Color color = (Color) Enum.Parse(typeof(Color), [1], true);
Drag options to blanks, or click blank then click option'
A"Green"
B"green"
CGreen
Dgreen
Attempts:
3 left
💡 Hint
Common Mistakes
Passing unquoted strings causes compile errors.
Passing the enum name without quotes.
4fill in blank
hard

Fill both blanks to parse "Yellow" ignoring case and assign to variable.

C Sharp (C#)
Color color = (Color) Enum.Parse(typeof(Color), [1], [2]);
Drag options to blanks, or click blank then click option'
A"Yellow"
Bfalse
Ctrue
DYellow
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the string.
Using false instead of true for ignoreCase.
5fill in blank
hard

Fill all three blanks to parse string to enum, check success, and print result.

C Sharp (C#)
if (Enum.TryParse<Color>([1], out Color [2]))
{
    Console.WriteLine([3]);
}
Drag options to blanks, or click blank then click option'
A"Purple"
BparsedColor
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted strings.
Mismatching variable names between out parameter and print statement.