C#.NET — Determine switch output when switching on an enum value. namespace CuriousTabConsoleApplication { public enum color { red, green, blue }; class SampleProgram { static void Main(string[] args) { color c = color.blue; switch (c) { case color.red: Console.WriteLine(color.red); break; case color.green: Console.WriteLine(color.green); break; case color.blue: Console.WriteLine(color.blue); break; } } } }

Difficulty: Easy

Correct Answer: blue

Explanation:


Introduction / Context:
This question checks how enums print when written to the console and confirms correct case matching in a switch statement over an enum.


Given Data / Assumptions:

  • Enum color { red, green, blue } with implicit values 0, 1, 2.
  • Variable c is assigned color.blue.
  • Each switch case prints the enum member.


Concept / Approach:
Console.WriteLine on an enum value writes the identifier name by default (not the underlying numeric value), unless formatted otherwise. Matching case color.blue triggers printing of "blue".


Step-by-Step Solution:

Switch discriminant is color.blue. Case color.blue matches; Console.WriteLine(color.blue) executes. The console prints the enum member name: "blue".


Verification / Alternative check:
If you cast to int, e.g., (int)color.blue, you would see 2. Without casting, the name is printed.


Why Other Options Are Wrong:

  • "red" and numeric outputs 0/1/2 do not occur because the code prints the identifier, not the underlying integer.
  • "blue" is the only branch that runs.


Common Pitfalls:
Assuming enums print as numbers by default or believing switch falls through and prints multiple lines (it does not due to break statements).


Final Answer:
blue

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion