Difficulty: Easy
Correct Answer: The compiler will report case i and case j as errors since variables cannot be used in cases.
Explanation:
Introduction / Context:
This question checks the requirement that C# switch case labels be compile-time constants, not variables whose values are only known at runtime.
Given Data / Assumptions:
Concept / Approach:
In C#, case labels must be constant expressions (e.g., literals, const fields, or enum members). Using non-const variables as case labels is a compile-time error.
Step-by-Step Solution:
Verification / Alternative check:
Replace i or j with const int K = 5; case K: ... and the code would compile. Similarly, enum members are allowed because they are constants.
Why Other Options Are Wrong:
Common Pitfalls:
Confusing switch-case rules from other languages or assuming any expression is permitted in labels. C# enforces constants for determinism at compile time.
Final Answer:
The compiler will report case i and case j as errors since variables cannot be used in cases.
Discussion & Comments