DEV Community

Cover image for Day 3 of 30-Day .NET Challenge: Switch Constructs
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published at Medium

1 1 1 1 1

Day 3 of 30-Day .NET Challenge: Switch Constructs

Introduction

The switch statements are available for creating branching logic, each offering distinct advantages based on readability and maintenance.

Learning Objectives

  • Utilize the switch-case construct to compare a variable or expression with multiple potential outcomes.

Prerequisites for Developers

  • Utilizing the if-else construct to incorporate branching logic.

  • Handling variables, employing string interpolation, and displaying output.

Getting Started

What is a switch statement?

The switch statement selects and executes a specific section of code from a list of options known as switch sections. This selection is made by matching the switch expression with predefined patterns in the switch sections.

Basic Example

switch (fruit)
{
    case "apple":
        Console.WriteLine($"App will display information for apple.");
        break;

    case "banana":
        Console.WriteLine($"App will display information for banana.");
        break;

    case "cherry":
        Console.WriteLine($"App will display information for cherry.");
        break;
}
Enter fullscreen mode Exit fullscreen mode

Basic Switch Example

To begin, create a static class file called “Switch.cs” within the console application. Insert the provided code snippet into this file.

static int employeeLevel = 200;
static string employeeName = "John Smith";

/// <summary>
/// Outputs
/// John Smith, Senior Associate
/// </summary>
public static void SwitchExample()
{
    string title = "";

    switch (employeeLevel)
    {
        case 100:
            title = "Junior Associate";
            break;
        case 200:
            title = "Senior Associate";
            break;
        case 300:
            title = "Manager";
            break;
        case 400:
            title = "Senior Manager";
            break;
        default:
            title = "Associate";
            break;
    }

    Console.WriteLine($"{employeeName}, {title}");
}
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

#region Day 3 - Switch Constructs

Switch.SwitchExample();

#endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

// Console Output
John Smith, Senior Associate
Enter fullscreen mode Exit fullscreen mode

Change Switch Label

Add another method into the same static class as shown below

static int employeeLevel = 200;
static string employeeName = "John Smith";

public static void SwitchExample()
{
    string title = "";

    switch (employeeLevel)
    {
        case 100:
            title = "Junior Associate";
            break;
        case 200:
            title = "Senior Associate";
            break;
        case 300:
            title = "Manager";
            break;
        case 400:
            title = "Senior Manager";
            break;
        default:
            title = "Associate";
            break;
    }

    Console.WriteLine($"{employeeName}, {title}");
}

/// <summary>
/// John Smith, Associate
/// </summary>
public static void ChangeSwitchLabelExample()
{
    employeeLevel = 201;
    SwitchExample();
}
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

#region Day 3 - Switch Constructs

Switch.ChangeSwitchLabelExample();

#endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

// Console Output
John Smith, Associate
Enter fullscreen mode Exit fullscreen mode

Multiple Switch Labels

Add another method into the same static class as shown below

    /// <summary>
    /// Outputs
    /// John Smith, Senior Associate
    /// </summary>
    public static void MultipleSwitchLabelExample()
    {
        int employeeLevel = 100;
        string employeeName = "John Smith";

        string title = "";

        switch (employeeLevel)
        {
            case 100:
            case 200:
                title = "Senior Associate";
                break;
            case 300:
                title = "Manager";
                break;
            case 400:
                title = "Senior Manager";
                break;
            default:
                title = "Associate";
                break;
        }

        Console.WriteLine($"{employeeName}, {title}");
    }
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

    #region Day 3 - Switch Constructs

    Switch.MultipleSwitchLabelExample();

    #endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

    // Console Output
    John Smith, Senior Associate
Enter fullscreen mode Exit fullscreen mode

Complete Code on GitHub

GitHub — ssukhpinder/30DayChallenge.Net

C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

If you’ve made it this far, please show your appreciation with a clap and follow the author! 👏️️

Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr

Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev

More content at C# Programming

Top comments (0)

Agent.ai Challenge image

Congrats to the Agent.ai Challenge Winners 🏆

The wait is over! We are excited to announce the winners of the Agent.ai Challenge.

From meal planners to fundraising automators to comprehensive stock analysts, our team of judges hung out with a lot of agents and had a lot to deliberate over. There were so many creative and innovative submissions, it is always so difficult to select our winners.

Read more →

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay