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

Why enums are needed in C Sharp (C#) - See It in Action

Choose your learning style9 modes available
Why enums are needed
📖 Scenario: Imagine you are building a simple program to track the days of the week for a calendar app. You want to store and use days like Monday, Tuesday, and so on.
🎯 Goal: You will create a program that uses an enum to represent days of the week. This will help you understand why enums are useful for grouping related named values.
📋 What You'll Learn
Create an enum called DayOfWeek with all seven days as named values
Create a variable called today and set it to DayOfWeek.Wednesday
Write a switch statement using today to print a message for the day
Print the message to the console
💡 Why This Matters
🌍 Real World
Enums are used in many programs to represent fixed sets of options, like days, colors, or states.
💼 Career
Understanding enums helps you write clear and maintainable code, a skill valued in software development jobs.
Progress0 / 4 steps
1
Create the enum DayOfWeek
Create an enum called DayOfWeek with these exact values: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday.
C Sharp (C#)
Need a hint?

An enum groups related named values. Use the enum keyword followed by the name and curly braces with the days inside.

2
Create a variable today of type DayOfWeek
Create a variable called today of type DayOfWeek and set it to DayOfWeek.Wednesday.
C Sharp (C#)
Need a hint?

Use the enum name as the type and assign the value with the enum name and dot notation.

3
Use a switch statement to print a message for today
Write a switch statement using the variable today. For DayOfWeek.Wednesday, print "It's Wednesday, midweek!". For other days, print "It's not Wednesday.".
C Sharp (C#)
Need a hint?

Use switch with case for Wednesday and default for other days. Use System.Console.WriteLine to print.

4
Run the program to see the output
Run the program and print the message from the switch statement to the console.
C Sharp (C#)
Need a hint?

Running the program should print the message for Wednesday.