C# Beginners – Switch Statement

C# Logo = C# Beginners - Switch Statement
C# Logo

Introduction

C# Beginners – Switch Statement; covers using switch statements to make decisions in C#. The tutorial builds on using if-else selection. Before attempting this tutorial, you should have chosen an IDE and be comfortable with it. Additionally, you should be familiar with the essential data types and conditional statements for making a selection. If this is not the case, please see previous tutorials in this series. There is a video version at the bottom of this post for anyone that prefers a video tutorial. It is also available by clicking here.

What Are Switch Statements – C# Beginners – Switch Statement

Switch statements are used when there are a finite number of fixed possible outcomes. An easy example is if we want to select an action for each day of the week. There are seven days a week, so the selection is always based on one of seven possible values.

See the following example;

Console.WriteLine("please enter the day of the week");
string? day  = Console.ReadLine();
day = day.Trim().ToLower();
switch (day)
{
    case "monday":
        Console.WriteLine("Today is Monday");
        break;
    case "tuesday":
        Console.WriteLine("Today is Tuesday");
        break;
    case "wednesday":
        Console.WriteLine("Today is Wednesday");
        break;
    case "thursday":
        Console.WriteLine("Today is Thursday");
        break;
    case "friday":
        Console.WriteLine("Today is Friday");
        break;
    case "saturday":
        Console.WriteLine("Today is Saturday");
        break;
    case "sunday":
        Console.WriteLine(("Today is sunday"));
        break;
    default:
        Console.WriteLine("Sorry that is not a known day of the week");
        break;
}

In the example above, a user enters a day of the week. Since it is a console entry, there are rudimentary steps to sanitise the user input. First, the code changes the entire string to lowercase, and then it removes white space from the start and end. Next, the actual switch statement begins.

Initially, it starts with the word “switch”, and we pass the variable to switch on into the parameters. Next, the rest goes inside a code block. Finally, each possibility gets a case. In this example, all cases are in speech marks because it is switching on a string.

The default case can detect erroneous situations (if wrong input is possible); however, if not, the default case can cover the last possible situation.

Console.WriteLine("please enter the day of the week");
string? day  = Console.ReadLine();
day = day.Trim().ToLower();
switch (day)
{
    case "monday":
        Console.WriteLine("Today is Monday");
        break;
    case "tuesday":
        Console.WriteLine("Today is Tuesday");
        break;
    case "wednesday":
        Console.WriteLine("Today is Wednesday");
        break;
    case "thursday":
        Console.WriteLine("Today is Thursday");
        break;
    case "friday":
        Console.WriteLine("Today is Friday");
        break;
    case "saturday":
        Console.WriteLine("Today is Saturday");
        break;
    default:
        Console.WriteLine(("Today is sunday"));
        break;
}

Switch statements can use value types such as integers and doubles. More commonly, they use enums. In C#, each case can have multiple lines of code and must have a break statement to end the case.

Enums – C# Beginners – Switch Statement

Enums are a data structure suited to things like days of the week. In the background, they work as a value type but are more accessible to read by humans. Declaring an enum with top-level statements differs slightly from the older way of starting with C#. The basis is still the same, but the positioning is different.

Declaring an Enum

enum Day{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}

After the enum values are declared, an actual instance of the enum is made :

Day day = Day.Thursday;

This is where things differ; both examples are below. With top-level statements, the enum declaration must come after most other statements.

Top Level Statements Example

// See https://aka.ms/new-console-template for more information

Day day = Day.Thursday;

switch (day)
{
    case Day.Monday:
        Console.WriteLine("Today is Monday");
        break;
    case Day.Tuesday:
        Console.WriteLine("Today is Tuesday");
        break;
    case Day.Wednesday:
        Console.WriteLine("Today is Wednesday");
        break;
    case Day.Thursday:
        Console.WriteLine("Today is Thursday");
        break;
    case Day.Friday:
        Console.WriteLine("Today is Friday");
        break;
    case Day.Saturday:
        Console.WriteLine("Today is Saturday");
        break;
    default:
        Console.WriteLine(("Today is sunday"));
        break;
}

enum Day{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}

Older Way 1

namespace ConsoleApp3
{
    internal class Program
    {
        enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }

        static void Main(string[] args)
        {
            Day day = Day.Thursday;
            switch (day)
            {
                case Day.Monday:
                    Console.WriteLine("Today is Monday");
                    break;
                case Day.Tuesday:
                    Console.WriteLine("Today is Tuesday");
                    break;
                case Day.Wednesday:
                    Console.WriteLine("Today is Wednesday");
                    break;
                case Day.Thursday:
                    Console.WriteLine("Today is Thursday");
                    break;
                case Day.Friday:
                    Console.WriteLine("Today is Friday");
                    break;
                case Day.Saturday:
                    Console.WriteLine("Today is Saturday");
                    break;
                default:
                    Console.WriteLine(("Today is sunday"));
                    break;
            }
        }
    }
}

Older Way 2

namespace ConsoleApp3
{
    enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }

    internal class Program
    {
        static void Main(string[] args)
        {
            Day day = Day.Thursday;
            switch (day)
            {
                case Day.Monday:
                    Console.WriteLine("Today is Monday");
                    break;
                case Day.Tuesday:
                    Console.WriteLine("Today is Tuesday");
                    break;
                case Day.Wednesday:
                    Console.WriteLine("Today is Wednesday");
                    break;
                case Day.Thursday:
                    Console.WriteLine("Today is Thursday");
                    break;
                case Day.Friday:
                    Console.WriteLine("Today is Friday");
                    break;
                case Day.Saturday:
                    Console.WriteLine("Today is Saturday");
                    break;
                default:
                    Console.WriteLine(("Today is sunday"));
                    break;
            }
        }
    }
}

Which To use?

At the start, I stated that, since top-level statements are the new default, that is the way to go. However, typically enums are declared outside the class (“older way 2” example). Classes will be covered later in the series, but you will likely see enums created in multiple ways in other examples. I feel it is beneficial to at least see everything there now.

Video Version

Switch Statements Video Tutorial

Related Articles – C Beginners – Switch Statement

Related Post

11 thoughts on “C# Beginners – Switch Statement

Leave a Reply

Your email address will not be published. Required fields are marked *