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

Enum with switch pattern 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 declare an enum named Color.

C Sharp (C#)
enum [1] { Red, Green, Blue }
Drag options to blanks, or click blank then click option'
Acolor
BColors
CColor
DColour
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural form like 'Colors' instead of singular 'Color'.
Using lowercase names which is not a C# convention.
2fill in blank
medium

Complete the switch expression to return the string name of the color.

C Sharp (C#)
string GetColorName(Color color) => color switch { Color.Red => [1], _ => "Unknown" };
Drag options to blanks, or click blank then click option'
ARed
B"Red"
C'Red'
DRed.ToString()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the enum member itself instead of a string.
Using single quotes which are for characters, not strings.
3fill in blank
hard

Fix the error in the switch expression to handle all enum values.

C Sharp (C#)
string GetColorName(Color color) => color switch { Color.Red => "Red", Color.Green => [1], Color.Blue => "Blue", _ => "Unknown" };
Drag options to blanks, or click blank then click option'
Acolor.ToString()
BGreen
C'Green'
D"Green"
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the enum member instead of a string.
Using single quotes instead of double quotes.
4fill in blank
hard

Fill both blanks to complete the switch expression that returns a message for each color.

C Sharp (C#)
string GetColorMessage(Color color) => color switch { Color.Red => [1], Color.Green => [2], _ => "Color not recognized" };
Drag options to blanks, or click blank then click option'
A"Stop!"
B"Go!"
C"Wait!"
D"Slow down!"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up messages for colors.
Forgetting to use quotes around the messages.
5fill in blank
hard

Fill all three blanks to create a switch expression that returns the color's hex code.

C Sharp (C#)
string GetColorHex(Color color) => color switch { Color.Red => [1], Color.Green => [2], Color.Blue => [3], _ => "#000000" };
Drag options to blanks, or click blank then click option'
A"#FF0000"
B"#00FF00"
C"#0000FF"
D"#FFFFFF"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up hex codes for colors.
Using quotes incorrectly or missing them.