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

Switch expression (modern C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Switch Expression in C#
📖 Scenario: You are building a simple program that assigns a description to a day of the week. This helps users understand what kind of day it is.
🎯 Goal: Create a program that uses a modern C# switch expression to convert a day name into a short description.
📋 What You'll Learn
Create a string variable called day with a day name.
Create a switch expression called description that matches day to a description.
Use the switch expression to assign descriptions for Monday, Friday, and Sunday.
Print the description to the console.
💡 Why This Matters
🌍 Real World
Switch expressions help write clear and concise code for decision making, such as mapping inputs to outputs in apps.
💼 Career
Understanding switch expressions is useful for C# developers to write modern, readable, and maintainable code.
Progress0 / 4 steps
1
Create the day variable
Create a string variable called day and set it to the value "Monday".
C Sharp (C#)
Need a hint?

Use string day = "Monday"; to create the variable.

2
Create the description switch expression
Create a string variable called description and use a switch expression on day to assign:
- "Start of work week" for "Monday"
- "Almost weekend" for "Friday"
- "Rest day" for "Sunday"
Use _ as the default case with "Regular day".
C Sharp (C#)
Need a hint?

Use the syntax: string description = day switch { "Monday" => "Start of work week", ... };

3
Change day to "Friday"
Change the value of the day variable to "Friday".
C Sharp (C#)
Need a hint?

Just replace the value inside the quotes: string day = "Friday";

4
Print the description
Write a Console.WriteLine statement to print the value of description.
C Sharp (C#)
Need a hint?

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