Syntax
switch (value) {\n case option:\n // code\n break;\n default:\n // code\n}Examples
Traditional switch Statement
The classic switch syntax, requiring break to prevent fall-through.
int day = 3;
string dayName;
switch (day)
{
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Unknown";
break;
}
Console.WriteLine(dayName); // WednesdayGrouping Cases
Multiple case labels can share the same code block by stacking them.
int month = 4;
string season;
switch (month)
{
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
default:
season = "Other";
break;
}
Console.WriteLine(season); // SpringModern switch Expression
Using arrow syntax to return a value directly - more concise, and no fall-through risk.
int day = 6;
string dayType = day switch
{
1 or 2 or 3 or 4 or 5 => "Weekday",
6 or 7 => "Weekend",
_ => "Invalid day"
};
Console.WriteLine(dayType); // WeekendPattern Matching in switch
Modern switch expressions can match on type and shape, not just exact values.
object item = 42;
string description = item switch
{
int n when n > 0 => $"Positive number: {n}",
int n when n < 0 => $"Negative number: {n}",
int => "Zero",
string s => $"A string: {s}",
null => "Nothing here",
_ => "Unknown type"
};
Console.WriteLine(description); // Positive number: 42Best practices
- Never forget break in a traditional switch statement - a missing break causes execution to fall through into the next case
- Prefer the modern switch expression (C# 8+) when your target version supports it - it removes the fall-through pitfall and is more concise
- Always include a default case (or _ in a switch expression) to handle unexpected values gracefully
- Use switch over a long else if chain when comparing one variable against several specific, discrete values or patterns
At a glance
- Purpose
- Applications on the .NET platform
- File extension
- .cs
- Runs in
- .NET runtime
- Usually used with
- .NET SDK and libraries
Specifications & further reading
Related C# documentation
Variables & Data Types
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.