📖 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#)
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#)
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#)
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#)
Hint
Running the program should print the message for Wednesday.
Practice
(1/5)
1. Why do we use enum types in C#?
easy
A. To create variables that can hold any data type
B. To group related named constants for better code clarity
C. To perform mathematical calculations faster
D. To store large amounts of text data efficiently
Solution
Step 1: Understand the purpose of enums
Enums group related named constants, making code easier to read and maintain.
Step 2: Compare other options
Other options describe unrelated uses like variable types or data storage, which enums do not do.
Final Answer:
To group related named constants for better code clarity -> Option B
Quick Check:
Enums = group named constants [OK]
Hint: Enums group related names, not data or calculations [OK]
Common Mistakes:
Thinking enums store any data type
Confusing enums with variables or arrays
Assuming enums improve performance directly
2. Which of the following is the correct way to declare an enum in C#?
easy
A. enum Colors = (Red, Green, Blue);
B. enum Colors { 'Red', 'Green', 'Blue' };
C. enum Colors: Red, Green, Blue;
D. enum Colors { Red, Green, Blue };
Solution
Step 1: Recall enum declaration syntax
In C#, enums are declared with enum Name { Value1, Value2, ... } syntax.
Step 2: Check each option
enum Colors { Red, Green, Blue }; matches correct syntax; others use invalid symbols or punctuation.