Abdullah  Kozey

Abdullah Kozey

1624679138

All the ways you can use switch in C# today

The switch statement has been a staple control statement in basically every programming language for as long as we can remember.

It’s allowing (mostly) better performance than a lot of nested regular if statements, and also heavily increases readability as well as debuggability due to reduced nesting.

However, with recent C## versions, there have been lots of additions around the switch keyword in very quick succession, so I’d like to show you a small overview of every way you can use switch in C## by now (given you’re using the latest version, which is C## 9 at the time of writing this).

Up to C## 7

The classic switch statement we are all familiar with:

string value = "Value";

switch (value) 
{
    case "Value":
        Console.WriteLine("Hello");
        break;
    case "Other":
    default:
        Console.WriteLine("Medium");
        break;
}
// Result: "Hello"

It’s simple, you pass a value to the switch, and then match it with the available case statements. Depending on that, the matching block is executed.

There’s some room for more sophisticated flows by e.g. putting case statements on top of each other without a break in between so a “fall-through” takes place. But that’s about everything you can do with it. Also, it is possible to include _goto _statements to execute multiple case blocks.

C## 7

In C## 7, switch was extended a bit. Now it is possible to use a switch to perform more sophisticated pattern matching, e.g for a specific type. An example:

object someType = "";

switch (someType) 
{
    case string:
        Console.WriteLine("String");
        break;
    case IEnumerable<string>:
        Console.WriteLine("IEnumerable<string>");
        break;
}
Result: "String"

This will print “String”, since the type behind the object is a string. This can be especially useful with a generic method for example, which should act according to the type it’s being passed.

In addition, C## 7 allows a switch when pattern to further enhance this:

object someType = "Hello";
switch (someType) 
{
    case 5:
        Console.WriteLine("5");
        break;
    case "Hello":
        Console.WriteLine("Hello");
        break;
    case string s when s.StartsWith("Hel"):
        Console.WriteLine("StartsWithEll");
        break;
    case string s when s.Length == 3:
        Console.WriteLine("StartsWithHel");
        break;
}
// Result: "Hello" (Since it's the first match. Otherwise "StartsWithEll" would match.

This switch will try to cast the value for every case. If the cast is not null (As you would do with an is statement), you can then perform checks on the value. If these match, then your case will be executed. If there are multiple matches, the first one will execute.

Note that you can even combine concrete values and more elaborate checks in the switch. I was able to e.g. use a concrete int as well as a concrete string together with **when **based patterns!

#c-sharp-programming #dotnet #c-sharp-language #csharp #c#

All the ways you can use switch in C# today