Skip to content

Pattern Matching (C# 7+)

object o = 123;
if (o is int n && n > 100)
{
Console.WriteLine($"Large number: {n}");
}
string Size(int x) => x switch
{
< 0 => "neg",
0 => "zero",
< 10 => "small",
_ => "big"
};

Notes

  • Use property and relational patterns for expressive code.
  • Prefer switch expressions for concise mappings.