New Switch syntax with C# 8.0

Microsoft has launched a newer syntax for switch expressions 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.

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 unknwn.");
	  break;
}

gist https://gist.github.com/ssukhpinder/33f204f3fda8ab97326b2b06a091405d

New switch syntax advantages

Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.

Follow on following channels to stay tuned on upcoming stories on C#

C# Publication, LinkedIn, Instagram, Twitter, Dev.to, Pinterest, Substack, Wix