C# CONCEPTS

New Switch syntax with C# 8.0

Microsoft has launched newer syntax for switch expression starting from C# 8.0. Switch statements must produce a value in each of its case blocks. Switch expressions allow us to use extra compact expression syntax.

Sukhpinder Singh | .Net Developer
C# Programming
Published in
1 min readAug 23, 2020

--

With new syntax are fewer repetitive “cases and break” keywords and fewer “curly braces.”

As an example, consider the following enum with a collection of the colors:

public enum Color {Red,     
Orange,
Yellow,
Green,
Blue,
Indigo,
Violet
}

Old switch syntax

switch (c)
{
case Color.Red:
Console.WriteLine("The color is red");
break;
default:
Console.WriteLine("The color is unknown.");
break;
}

New Switch Syntax — RECOMMENDED

New switch syntax…

--

--

Sukhpinder Singh | .Net Developer
C# Programming

Programmer by heart | C# | .Net 6 | .Net 8 | dotnet | dotnetcore | Kubernetes | Angular | AWS | Remember to 👏 Linktree: https://linktr.ee/sukhpindersingh