-
Notifications
You must be signed in to change notification settings - Fork 2
CSharp Basics Switch PatternMatching
Joe Care edited this page Dec 22, 2025
·
1 revision
This lesson introduces simple pattern matching with switch expressions and statements in C#.
Related wiki pages:
- C# overview:
CSharpBible.md
Official docs:
- Pattern matching: https://learn.microsoft.com/dotnet/csharp/fundamentals/functional/pattern-matching
using System;
int value = 5;
string description = value switch
{
< 0 => "Negative",
0 => "Zero",
1 => "One",
<= 10 => "Between 2 and 10",
_ => "Greater than 10"
};
Console.WriteLine(description);object input = "hello";
string result = input switch
{
int i => $"Integer: {i}",
string s => $"String length: {s.Length}",
null => "Null value",
_ => "Unknown type"
};
Console.WriteLine(result);- Explore more advanced patterns (property, positional) in the official docs.
- Combine pattern matching with exception handling and domain models from other lessons in this wiki.