Syntax

C# Switch Expression Syntax

In C#, switch expression: returns a value based on patterns — var label = n switch { 1 => "one", _ => "other" };

Quick Answer

Syntax
var label = n switch { 1 => "one", _ => "other" };
C#Syntax

Switch Expression

Code example
var label = n switch { 1 => "one", _ => "other" };

Returns a value based on patterns.

Learn More

How to switch expression in C#

  1. Open your C# file or editor.
  2. Copy the example, then replace its sample names and values for your code.

C# Switch Expression Syntax

Use var label = n switch { 1 => "one", _ => "other" };.

Related Syntax References

Frequently asked questions

What is the C# syntax for switch expression?

Use this pattern: var label = n switch { 1 => "one", _ => "other" };

Can I copy this C# code example?

Yes. Use Copy code and adapt the example to your program.

Is this C# syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify