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

Why enums are needed in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of enum usage in C#

What is the output of this C# code that uses an enum to represent days?

C Sharp (C#)
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }

class Program {
    static void Main() {
        Day today = Day.Wednesday;
        Console.WriteLine((int)today);
    }
}
AWednesday
B4
C3
D2
Attempts:
2 left
💡 Hint

Remember enums start numbering from 0 by default.

🧠 Conceptual
intermediate
1:30remaining
Why use enums instead of constants?

Why are enums preferred over using multiple constant values for related options?

AEnums group related named constants and improve code readability and type safety.
BEnums allow storing multiple values in one variable simultaneously.
CEnums automatically generate user interfaces for options.
DEnums are faster than constants in execution.
Attempts:
2 left
💡 Hint

Think about how enums help organize and check values in code.

Predict Output
advanced
2:00remaining
Enum underlying type and output

What is the output of this C# code that sets an enum underlying type to byte?

C Sharp (C#)
enum Status : byte { Off = 0, On = 1, Unknown = 255 }

class Program {
    static void Main() {
        Status s = Status.Unknown;
        Console.WriteLine((int)s);
    }
}
A1
BUnknown
C0
D255
Attempts:
2 left
💡 Hint

Check the assigned value and how casting works.

🔧 Debug
advanced
2:00remaining
Identify the error with enum assignment

What error occurs when running this C# code?

C Sharp (C#)
enum Color { Red, Green, Blue }

class Program {
    static void Main() {
        Color c = 5;
        Console.WriteLine(c);
    }
}
ACompile-time error: Cannot implicitly convert int to Color
BRuntime error: Invalid enum value
COutput: 5
DOutput: Blue
Attempts:
2 left
💡 Hint

Check how enum variables are assigned values.

🚀 Application
expert
2:30remaining
Using enums to improve code safety

Given this method that takes an enum parameter, what is the benefit of using an enum here?

enum Direction { North, East, South, West }

void Move(Direction dir) {
    Console.WriteLine($"Moving {dir}");
}
AIt allows passing any integer value to the method without errors.
BIt restricts input to valid directions, preventing invalid values and improving code clarity.
CIt automatically converts directions to coordinates internally.
DIt makes the method run faster by using enums.
Attempts:
2 left
💡 Hint

Think about how enums limit what values can be passed.