How Does the Switch Statement Work in C#?

πŸ’‘ Concept: Switch Statement in C#

The switch statement in C# is used for decision-making. It evaluates an expression and executes the matching case block based on the result, improving readability over multiple if-else conditions.

πŸ“˜ Quick Intro

switch is a control flow statement that selects a code block to execute based on the value of an expression. It supports patterns and multiple case labels, and in modern C#, supports enhanced match expressions.

🧠 Analogy

Think of a vending machine. You press a button (input), and it delivers a specific item (case block). The machine doesn’t check all itemsβ€”just the one mapped to your input. That’s how switch optimizes flow.

πŸ”§ Technical Explanation

  • Works with types like int, char, string, enum, and patterns (C# 7+).
  • Each case must end with break, return, or control flow redirection.
  • default handles unmatched cases.
  • Pattern matching and when clauses enable more powerful use in modern C#.
  • Fall-through behavior is not allowed unless using pattern matching or goto case.

🎯 Use Cases

  • Menu selection or command execution.
  • Mapping enums to user-readable output.
  • Routing logic in desktop or web applications.
  • Reducing complexity in nested if-else chains.

πŸ’» Real Code Example

int day = 3;
switch (day)
{
    case 1:
        Console.WriteLine("Monday");
        break;
    case 2:
        Console.WriteLine("Tuesday");
        break;
    case 3:
        Console.WriteLine("Wednesday");
        break;
    default:
        Console.WriteLine("Unknown day");
        break;
}

❓ Interview Q&A

Q1: What is a switch statement?
A: A control flow tool that executes one block of code based on a value match.

Q2: When is switch preferred over if-else?
A: When checking one variable against multiple constant values.

Q3: Can switch work with strings?
A: Yes, since C# 7.0.

Q4: What happens if no case matches?
A: The default block runs, if defined.

Q5: Is fall-through allowed like in C or Java?
A: No, each case must end with break or similar.

Q6: Can multiple cases map to the same block?
A: Yes, by grouping them without a break.

Q7: What is pattern matching in switch?
A: Matching based on type and condition using is and when.

Q8: Can switch be used inside a method return?
A: Yes, especially in expression-bodied members (C# 8+).

Q9: What’s the purpose of the goto case?
A: It transfers control to another case label.

Q10: Can switch handle ranges?
A: Yes, using relational patterns in newer C# versions.

πŸ“ MCQs

Q1. Which types are supported in switch?

  • Only int
  • Only string
  • All objects
  • int, char, string, enum

Q2. What must each case end with?

  • continue
  • stop
  • break or return
  • pause

Q3. What is the purpose of default case?

  • Start first
  • Handle unmatched values
  • Compile faster
  • Ignore input

Q4. Can switch handle strings?

  • No
  • Only in Java
  • Yes
  • Only with enum

Q5. What happens without a break?

  • Fall-through
  • No output
  • Skip case
  • Compiler error

Q6. Is switch more efficient than if-else?

  • Always
  • Never
  • Sometimes
  • Only with enums

Q7. What does goto case do?

  • Restarts
  • Ends switch
  • Jumps to another case
  • None

Q8. When is switch preferred?

  • Nested loops
  • One condition
  • Multiple conditions on one variable
  • Class inheritance

Q9. Can switch use pattern matching?

  • No
  • Only Java
  • Yes, in modern C#
  • Only for arrays

Q10. What is printed if no match and no default?

  • Error
  • Null
  • Default value
  • Nothing

πŸ’‘ Bonus Insight

In modern C#, switch expressions allow more expressive logic using pattern matching, simplifying complex conditionals. They're ideal for compact returns in LINQ or APIs.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: