5️⃣ C# Control Flow & Decision Making
Estimated reading: 3 minutes 281 views

C# Switch Statement – Simplify Complex Decision Making in Modern C#


Introduction – Why Use the C# Switch Statement?

In modern application logic, especially when handling multiple conditional branches, the C# switch statement offers a cleaner, more readable alternative to deep if...else chains. It enhances code clarity, supports pattern matching, and improves performance in multi-branch scenarios.

In this guide, you’ll learn:

  • The syntax of switch and switch-case
  • Pattern matching in switch (C# 8+)
  • Use cases with strings, enums, and expressions
  • Best practices and common pitfalls

Core Concept – What is a C# Switch Statement?

A switch statement evaluates an expression and executes matching case blocks. It’s commonly used when comparing a single variable against a list of constants or patterns.

Basic Syntax:

switch (expression)
{
    case constant1:
        // Code block
        break;
    case constant2:
        // Code block
        break;
    default:
        // Default block (optional)
        break;
}

Code Example – Switch on Integer

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("Invalid day");
        break;
}

Output:
Wednesday

Explanation:

  • Checks day.
  • Matches case 3 and prints “Wednesday”.

Advanced Example – Switch with Strings

string command = "start";

switch (command)
{
    case "start":
        Console.WriteLine("Application starting...");
        break;
    case "stop":
        Console.WriteLine("Application stopping...");
        break;
    default:
        Console.WriteLine("Unknown command.");
        break;
}

Output:
Application starting...


Pattern Matching – C# 8+ Switch Expression

int score = 85;
string grade = score switch
{
    >= 90 => "A",
    >= 80 => "B",
    >= 70 => "C",
    >= 60 => "D",
    _     => "F"
};

Console.WriteLine($"Grade: {grade}");

Output:
Grade: B

Best Practice: Use pattern matching switch expressions for concise, readable logic.


Enums and Switch – Cleaner Logic

enum Status { Pending, Approved, Rejected }

Status applicationStatus = Status.Approved;

switch (applicationStatus)
{
    case Status.Pending:
        Console.WriteLine("Awaiting review.");
        break;
    case Status.Approved:
        Console.WriteLine("Application approved.");
        break;
    case Status.Rejected:
        Console.WriteLine("Application rejected.");
        break;
}

Output:
Application approved.


Comparison Table – If-Else vs Switch

Featureif-elseswitch
Best forComplex, nested conditionsMultiple constant value comparisons
Supports pattern match C# 7+ C# 8+ (more readable)
ReadabilityPoor with many conditionsHigh when branching is based on one var
PerformanceSlightly slower in long chainsOptimized by compiler into lookup table

Real-World Use Cases

  • Game menu inputs (start, load, exit)
  • Invoice status handling (draft, sent, paid)
  • Chatbot response routing
  • Day/week/month-based reports
  • Role-based access (Admin, Editor, Viewer)

Summary – Recap & Next Steps

Key Takeaways:

  • switch simplifies branching logic over single expressions.
  • Use switch expressions for clean, functional-style logic.
  • Always include default to handle unexpected cases.

Real-world relevance: Used in UI menus, status handling, server routing logic, and more.


FAQ Section

When should I use switch over if-else in C#?
Use switch when comparing a single variable to many constants. It improves readability and performance.


Can I use switch with strings and enums?
Yes. Strings, enums, chars, integers, and pattern expressions (C# 8+) are all valid.


What is the difference between switch statement and switch expression?
Statement is procedural with break; expression is functional and returns a result (C# 8+).


Do I need to use break after each case in switch?
Yes, in classic switch statements. Without break, control falls through the next case.


Is switch faster than if-else?
In many cases, yes. Switch can be compiled into a jump table, making it more efficient.


Share Now :
Share

πŸ” C# Switch Statement / C# Nested Switch

Or Copy Link

CONTENTS
Scroll to Top