📖 Scenario: Imagine you are creating a simple program to manage the days of the week. You want to use an enum to represent the days so your code is clear and easy to read.
🎯 Goal: You will create an enum named DaysOfWeek with all seven days as its members. Then, you will print one of the days to see how enums work in C#.
📋 What You'll Learn
Create an enum called DaysOfWeek with members: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
Create a variable called today of type DaysOfWeek and set it to DaysOfWeek.Wednesday
Print the value of today to the console
💡 Why This Matters
🌍 Real World
Enums are used to represent fixed sets of related constants, like days, months, directions, or states in a program.
💼 Career
Understanding enums helps you write clearer and safer code, which is important in many software development jobs.
Progress0 / 4 steps
1
Create the enum DaysOfWeek
Write an enum declaration named DaysOfWeek with these members exactly: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.
C Sharp (C#)
Hint
Use the enum keyword followed by the name DaysOfWeek. Inside curly braces, list all days separated by commas.
2
Create a variable today of type DaysOfWeek
Create a variable called today of type DaysOfWeek and set it to DaysOfWeek.Wednesday.
C Sharp (C#)
Hint
Use the syntax: DaysOfWeek today = DaysOfWeek.Wednesday;
3
Print the value of today
Write a Console.WriteLine statement to print the value of the variable today.
C Sharp (C#)
Hint
Use Console.WriteLine(today); to print the enum value.
4
Run the program to see the output
Run the program and observe the output. It should print Wednesday.
C Sharp (C#)
Hint
The program should print Wednesday exactly.
Practice
(1/5)
1. What is the purpose of an enum in C#?
easy
A. To group related named constants together
B. To create a new class type
C. To define a method
D. To declare a variable
Solution
Step 1: Understand enum concept
An enum groups related named constants to make code clearer.
Step 2: Compare with other options
Enums are not classes, methods, or variables but a special type for constants.
Final Answer:
To group related named constants together -> Option A
Quick Check:
Enum = group constants [OK]
Hint: Enums group named constants, not variables or methods [OK]
Common Mistakes:
Thinking enum is a class
Confusing enum with method or variable
Assuming enum holds variable data
2. Which of the following is the correct syntax to declare an enum named Days with members Monday and Tuesday?
easy
A. enum Days: Monday, Tuesday;
B. enum Days = { Monday, Tuesday };
C. enum Days { Monday, Tuesday };
D. enum Days (Monday, Tuesday);
Solution
Step 1: Recall enum declaration syntax
Enums use curly braces with members separated by commas, no equals or parentheses.
Step 2: Check each option
enum Days { Monday, Tuesday }; matches correct syntax: enum Days { Monday, Tuesday };.
Final Answer:
enum Days { Monday, Tuesday }; -> Option C
Quick Check:
Enum syntax uses braces and commas [OK]
Hint: Use braces and commas for enum members [OK]
Common Mistakes:
Using equals sign (=) after enum name
Using parentheses instead of braces
Using colon (:) instead of opening brace
3. What is the output of this code?
enum Color { Red, Green, Blue };
Console.WriteLine((int)Color.Green);
medium
A. 0
B. 1
C. 2
D. Green
Solution
Step 1: Understand default enum values
Enum members start at 0 by default: Red=0, Green=1, Blue=2.
Step 2: Cast enum member to int
Printing (int)Color.Green outputs its numeric value 1.
Final Answer:
1 -> Option B
Quick Check:
Green = 1 by default [OK]
Hint: Enum members start at 0 unless assigned [OK]
Common Mistakes:
Assuming first member starts at 1
Printing enum name instead of int value
Confusing enum member order
4. Identify the error in this enum declaration:
enum Status
{
Started = 1,
InProgress = 2,
Completed = "Done"
};
medium
A. Enum members must be separated by semicolons
B. Missing semicolon after enum declaration
C. Enum name must be lowercase
D. Enum members cannot have string values
Solution
Step 1: Check enum member value types
Enum members must be integral types (int, byte, etc.), not strings.
Step 2: Analyze the given code
"Completed = \"Done\"" assigns a string, which is invalid.
Final Answer:
Enum members cannot have string values -> Option D
Quick Check:
Enum values must be integers [OK]
Hint: Enum values must be integers, not strings [OK]
Common Mistakes:
Assigning string values to enum members
Thinking semicolon is required after enum
Assuming enum names must be lowercase
5. How can you declare an enum Priority with members Low=1, Medium=5, and High=10 so that the values are not sequential?
hard
A. enum Priority { Low = 1, Medium = 5, High = 10 };
B. enum Priority { Low, Medium, High }
C. enum Priority { Low = 1; Medium = 5; High = 10 }
D. enum Priority = { Low = 1, Medium = 5, High = 10 }
Solution
Step 1: Assign explicit values to enum members
To set custom values, assign each member an integer with = and commas between.
Step 2: Check syntax correctness
enum Priority { Low = 1, Medium = 5, High = 10 }; uses correct syntax with commas and explicit values.
Final Answer:
enum Priority { Low = 1, Medium = 5, High = 10 }; -> Option A
Quick Check:
Use = and commas for custom enum values [OK]
Hint: Assign values with = and separate members by commas [OK]
Common Mistakes:
Using semicolons instead of commas
Omitting explicit values for custom numbers
Using equals sign after enum name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.