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

Enum with switch pattern in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Enum with switch pattern
📖 Scenario: You are creating a simple program to describe different weather conditions using an enum. You want to print a message based on the weather type.
🎯 Goal: Build a program that uses an enum for weather types and a switch expression to print a message for each weather.
📋 What You'll Learn
Create an enum called Weather with values Sunny, Rainy, Cloudy, and Snowy
Create a variable today of type Weather and set it to Weather.Sunny
Use a switch expression on today to get a message string
Print the message string
💡 Why This Matters
🌍 Real World
Enums and switch expressions are common in programs that need to handle fixed categories like weather, traffic lights, or user roles.
💼 Career
Understanding enums and switch expressions helps you write clear and maintainable code in many software development jobs.
Progress0 / 4 steps
1
Create the Weather enum
Create an enum called Weather with these exact values: Sunny, Rainy, Cloudy, and Snowy.
C Sharp (C#)
Need a hint?

An enum groups related named values. Use enum Weather { Sunny, Rainy, Cloudy, Snowy }.

2
Create a variable for today's weather
Create a variable called today of type Weather and set it to Weather.Sunny.
C Sharp (C#)
Need a hint?

Declare Weather today = Weather.Sunny; to set the current weather.

3
Use a switch expression to get a message
Create a string variable called message and assign it the result of a switch expression on today. Use these exact cases:
Weather.Sunny => "It's a bright sunny day!"
Weather.Rainy => "Don't forget your umbrella."
Weather.Cloudy => "It might rain later."
Weather.Snowy => "Time to build a snowman!"
C Sharp (C#)
Need a hint?

Use a switch expression with today switch { ... } to assign message.

4
Print the message
Write a Console.WriteLine statement to print the message variable.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(message); to show the message on screen.