C# Tutorial: Pattern Matching and switch
switch expressions, type patterns, property patterns, positional patterns, and guard clauses in C#.
Classic switch Statement
```csharp
string command = "start";
switch (command) {
case "start":
Console.WriteLine("Starting...");
break;
case "stop":
Console.WriteLine("Stopping...");
break;
default:
Console.WriteLine("Unknown command");
break;
}
```
---
switch Expression (C# 8+)
Returns a value — no `break` needed, exhaustive matching enforced:
```csharp
int code = 404;
string message = code switch {
200 => "OK",
201 => "Created",
400 => "Bad Request",
401 => "Unauthorized",
403 => "Forbidden",
404 => "Not Found",
>= 500 and <= 599 => "Server Error",
_ => "Unknown",
};
Console.WriteLine(message); // Not Found
```
---
Type Pattern Matching
```csharp
object obj = "hello";
string desc = obj switch {
int n => $"Integer: {n}",
string s => $"String of length {s.Length}",
double d => $"Double: {d:F2}",
null => "null value",
_ => "unknown type",
};
Console.WriteLine(desc); // String of length 5
```
---
Property Patterns
Match on an object's properties:
```csharp
record Point(int X, int Y);
string Quadrant(Point p) => p switch {
{ X: > 0, Y: > 0 } => "I",
{ X: < 0, Y: > 0 } => "II",
{ X: < 0, Y: < 0 } => "III",
{ X: > 0, Y: < 0 } => "IV",
_ => "Origin or axis",
};
Console.WriteLine(Quadrant(new Point(3, -2))); // IV
```
---
Positional Patterns (with Deconstruct)
```csharp
string Classify((int x, int y) point) => point switch {
(0, 0) => "origin",
(_, 0) => "x-axis",
(0, _) => "y-axis",
_ => "other",
};
```
---
when Guard Clauses
```csharp
static string Categorize(int n) => n switch {
< 0 => "negative",
0 => "zero",
var x when x % 2 == 0 => "positive even",
_ => "positive odd",
};
```
---
is Pattern in if Statements
```csharp
if (obj is string { Length: > 10 } s) {
Console.WriteLine($"Long string: {s}");
}
if (obj is not null) {
Console.WriteLine("Not null");
}
```
---
Congratulations!
You've completed the **C# track** on uByte! You know:
- Types, classes, interfaces, generics
- LINQ, async/await, error handling
- HTTP, JSON, testing, and pattern matching
Ready for more? Try **interview prep** problems in C# or earn your C# **certification**.
What you'll learn in this C# pattern matching and switch tutorial
This interactive C# tutorial has 5 hands-on exercises. Estimated time: 12 minutes.
- switch Expression — Map `int code = 200` to a string using a switch expression: `200 → "OK"`, `404 → "Not Found"`, `_ → "Unknown"`. Print th…
- Relational Patterns — Use a switch expression with relational patterns to print a grade for `int score = 88`: `>= 90 → "A"`, `>= 80 → "B"`, `>…
- Type Pattern Matching — Given `object obj = 42`, use a switch expression on the type: `int → "Integer"`, `string → "String"`, `_ → "Other"`. Pri…
- Property Pattern — Define `record Point(int X, int Y)`. Use a switch expression with property patterns to print `"Origin"` for `(0,0)`, `"X…
- Guard Clause (when) — Use a switch expression with a `when` guard: for `int n = 7`, return `"Seven"` when `n == 7`, `"Even"` when `n % 2 == 0`…
C# Pattern Matching and switch concepts covered
- Classic switch Statement
- switch Expression (C# 8+)
- Type Pattern Matching
- Property Patterns
- Positional Patterns (with Deconstruct)
- when Guard Clauses
- is Pattern in if Statements
- Congratulations!