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

Enum parsing from strings in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Enum parsing from strings
📖 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#)
Need a 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#)
Need a 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#)
Need a 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#)
Need a hint?

Use Console.WriteLine(parsedColor); to show the result.