Pattern Matching
Patterns combine type checks, extraction, and conditions without unsafe casts.
How it works
Inspect types and values with declarative patterns.
The switch expression handles null, positive integers, non-empty strings, and a final fallback in one exhaustive expression.
Runnable C# example
Program.cs
using System;
class Program
{
static string Describe(object? value) => value switch
{
null => "missing",
int number when number > 0 => $"positive {number}",
string { Length: > 0 } text => $"text: {text}",
_ => "other"
};
static void Main() => Console.WriteLine(Describe(12));
}
positive 12Practice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.