A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 4.2: Switch Statement

 The switch statement in C# provides a way to handle multiple conditions based on the value of an expression. It's a cleaner alternative to using multiple if-else if statements when you have several possible values to compare. Let's dive into the syntax and usage of the switch statement.

1. Basic Syntax:

using System;

class Program
{
    static void Main()
    {
        // Example: Switch statement to check the day of the week
        string dayOfWeek = "Monday";

        switch (dayOfWeek)
        {
            case "Monday":
                Console.WriteLine("It's the start of the week!");
                break;
            case "Friday":
                Console.WriteLine("TGIF! It's Friday!");
                break;
            case "Saturday":
            case "Sunday":
                Console.WriteLine("It's the weekend!");
                break;
            default:
                Console.WriteLine("It's a regular weekday.");
                break;
        }
    }
}

In this example:

  • The switch statement evaluates the value of dayOfWeek.
  • Each case represents a possible value for dayOfWeek.
  • If a match is found, the corresponding block of code executes.
  • The break statement exits the switch block. If omitted, execution will continue to the next case.

2. Switch Statement Features:

  • Fall-Through: Unlike some other languages, C# switch cases fall through by default. In the example, if dayOfWeek is "Saturday," it falls through to the "Sunday" case.

  • Multiple Case Values: You can combine cases if you want the same block of code to execute for multiple values.

3. Switch Expression (C# 8.0 and later):

Starting from C# 8.0, you can use switch expressions, which are more concise and allow for more expressive code:

using System;

class Program
{
    static void Main()
    {
        // Example: Switch expression to check the day of the week
        string dayOfWeek = "Monday";

        string result = dayOfWeek switch
        {
            "Monday" => "It's the start of the week!",
            "Friday" => "TGIF! It's Friday!",
            "Saturday" or "Sunday" => "It's the weekend!",
            _ => "It's a regular weekday.",
        };

        Console.WriteLine(result);
    }
}

Here, the switch expression assigns the result directly to a variable (result) without the need for a separate statement. The _ serves as a wildcard for unmatched cases.

4. Run the Program:

Press F5 or click the "Start Debugging" button to build and run your program. You should see the output based on the value of dayOfWeek.

5. Experiment:

  • Modify the value of dayOfWeek and observe how the switch statement handles different cases.
  • Try adding more cases and experimenting with fall-through behavior.

Congratulations! You've learned how to use the switch statement in C#. In the next tutorial, we'll explore functions and methods.