codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

8 / 60 topics
6Control Structures in C#7If-Else Statements8Switch Case Statements9For Loops10While Loops11Do-While Loops12Break and Continue Statements
Tutorials/C# Programming/Switch Case Statements
🔷C# Programming

Switch Case Statements

Updated 2026-04-20
3 min read

Introduction

In C#, control flow is a fundamental concept that allows developers to execute code based on specific conditions. One of the key constructs for managing control flow is the switch statement, which provides a concise way to handle multiple conditional branches based on the value of a variable or expression.

This tutorial will cover the syntax, usage, and best practices of switch case statements in C#. We'll explore how to use them effectively to make your code more readable and maintainable.

Basic Syntax

The basic syntax of a switch statement in C# is as follows:

switch (expression)
{
    case constant1:
        // Code block for constant1
        break;
    case constant2:
        // Code block for constant2
        break;
    default:
        // Default code block
        break;
}
  • Expression: This is the variable or expression whose value is evaluated.
  • Case Labels: Each case label specifies a constant value to compare with the expression.
  • Break Statement: The break statement terminates the switch block and exits the switch construct. Without it, execution will "fall through" to the next case.
  • Default Case: The default case is optional and executes if none of the other cases match.

Example

Here's a simple example that demonstrates how to use a switch statement:

using System;

class Program
{
    static void Main()
    {
        int number = 2;
        
        switch (number)
        {
            case 1:
                Console.WriteLine("Number is one.");
                break;
            case 2:
                Console.WriteLine("Number is two.");
                break;
            default:
                Console.WriteLine("Number is not one or two.");
                break;
        }
    }
}

In this example, the output will be "Number is two." because the value of number is 2.

Advanced Usage

Multiple Cases with a Single Code Block

You can group multiple cases to execute the same code block:

using System;

class Program
{
    static void Main()
    {
        char grade = 'B';
        
        switch (grade)
        {
            case 'A':
            case 'B':
            case 'C':
                Console.WriteLine("Pass");
                break;
            default:
                Console.WriteLine("Fail");
                break;
        }
    }
}

In this example, if grade is 'A', 'B', or 'C', the output will be "Pass".

Using Expressions in Switch Statements

C# 8.0 introduced pattern matching, which allows you to use more complex expressions and patterns in switch statements:

using System;

class Program
{
    static void Main()
    {
        object obj = "Hello";

        switch (obj)
        {
            case int i:
                Console.WriteLine($"Integer: {i}");
                break;
            case string s when s.Length > 5:
                Console.WriteLine("Long string");
                break;
            default:
                Console.WriteLine("Other type");
                break;
        }
    }
}

In this example, the switch statement checks if obj is an integer or a long string.

Best Practices

Use break Statements Properly

Always include break statements at the end of each case block to prevent fall-through. This can lead to unexpected behavior and bugs in your code.

// Incorrect usage
switch (number)
{
    case 1:
        Console.WriteLine("Number is one.");
    case 2:
        Console.WriteLine("Number is two."); // Fall-through
        break;
    default:
        Console.WriteLine("Number is not one or two.");
        break;
}

In this incorrect example, if number is 1, both "Number is one." and "Number is two." will be printed.

Use default Case

Always include a default case to handle unexpected values. This makes your code more robust and less prone to errors.

switch (day)
{
    case "Monday":
        Console.WriteLine("Start of the week.");
        break;
    case "Friday":
        Console.WriteLine("End of the workweek.");
        break;
    default:
        Console.WriteLine("Weekend or invalid day.");
        break;
}

Keep Cases Simple

Avoid complex logic within switch cases. If a case block becomes too long or complicated, consider refactoring it into separate methods.

// Complex case example
case "Admin":
    if (user.IsActive)
    {
        Console.WriteLine("Welcome, Admin.");
    }
    else
    {
        Console.WriteLine("Inactive Admin.");
    }
    break;

// Refactored example
case "Admin":
    HandleAdminUser(user);
    break;

Use switch for Enumerations

Switch statements are particularly useful when dealing with enumerations. They make the code more readable and maintainable.

enum Color { Red, Green, Blue }

class Program
{
    static void Main()
    {
        Color color = Color.Green;

        switch (color)
        {
            case Color.Red:
                Console.WriteLine("Stop");
                break;
            case Color.Green:
                Console.WriteLine("Go");
                break;
            case Color.Blue:
                Console.WriteLine("Caution");
                break;
        }
    }
}

Conclusion

Switch case statements are a powerful tool in C# for managing control flow based on the value of a variable or expression. By understanding their syntax, advanced usage, and best practices, you can write more efficient, readable, and maintainable code.

Remember to always include break statements, use a default case, keep cases simple, and consider using switch statements with enumerations for better readability.


PreviousIf-Else StatementsNext For Loops

Recommended Gear

If-Else StatementsFor Loops