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.

With new syntax are fewer repetitive…


This content originally appeared on DEV Community and was authored by Sukhpinder Singh

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.

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;
}

New Switch Syntax — RECOMMENDED

New switch syntax advantages

  • The variable name comes before the switch keyword starting syntax.

  • The distinctive order makes it simple to recognize the switch expression from the switch statement.

  • The “case” and “:” elements replaced with =>.

  • The default case is replaced with a “_” character.

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


This content originally appeared on DEV Community and was authored by Sukhpinder Singh


Print Share Comment Cite Upload Translate Updates
APA

Sukhpinder Singh | Sciencx (2021-05-29T08:28:55+00:00) New Switch syntax with C# 8.0. Retrieved from https://www.scien.cx/2021/05/29/new-switch-syntax-with-c-8-0/

MLA
" » New Switch syntax with C# 8.0." Sukhpinder Singh | Sciencx - Saturday May 29, 2021, https://www.scien.cx/2021/05/29/new-switch-syntax-with-c-8-0/
HARVARD
Sukhpinder Singh | Sciencx Saturday May 29, 2021 » New Switch syntax with C# 8.0., viewed ,<https://www.scien.cx/2021/05/29/new-switch-syntax-with-c-8-0/>
VANCOUVER
Sukhpinder Singh | Sciencx - » New Switch syntax with C# 8.0. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/29/new-switch-syntax-with-c-8-0/
CHICAGO
" » New Switch syntax with C# 8.0." Sukhpinder Singh | Sciencx - Accessed . https://www.scien.cx/2021/05/29/new-switch-syntax-with-c-8-0/
IEEE
" » New Switch syntax with C# 8.0." Sukhpinder Singh | Sciencx [Online]. Available: https://www.scien.cx/2021/05/29/new-switch-syntax-with-c-8-0/. [Accessed: ]
rf:citation
» New Switch syntax with C# 8.0 | Sukhpinder Singh | Sciencx | https://www.scien.cx/2021/05/29/new-switch-syntax-with-c-8-0/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.