---
title: "C# pattern matching with is and switch"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://iinventedlight.com/library/notes/csharp-pattern-matching.md
source_license: "CC BY 4.0 (dotnet/docs); restated in our own words"
attribution: "Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn, Pattern matching overview - C#, CC BY 4.0 (dotnet/docs); Microsoft Learn, Patterns - Pattern matching using the is and switch expressions. - C# reference, CC BY 4.0 (dotnet/docs)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run; only the fetched documentation pages were read."
category: coding-references
topic_area: "csharp: pattern matching"
tags: csharp, dotnet, pattern-matching, switch
sources:
  - id: S1
    title: "Pattern matching overview - C#"
    publisher: Microsoft Learn
    url: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
  - id: S2
    title: "Patterns - Pattern matching using the is and switch expressions. - C# reference"
    publisher: Microsoft Learn
    url: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
---
# C# pattern matching with is and switch

## Short answer
Use patterns in `is` expressions, `switch` statements and `switch` expressions to test types, constants, ranges and property values in one readable check. [S1][S2]

## Example
```csharp
string Size(object o) => o switch {
    int n when n < 0      => "negative",
    int n and < 10        => "small",
    string { Length: 0 }  => "empty",
    null                  => "null",
    _                     => "other"
};
```
Combine patterns with `and`, `or`, `not`. [S2]

## Claims
- id: C1
  claim: Patterns work in is, switch statements and switch expressions.
  source: S1
  evidence: "C# uses patterns in is expressions, switch statements, and switch expressions."
- id: C2
  claim: Patterns can be on the right side of is.
  source: S1
  evidence: "On the right side of the is operator."
- id: C3
  claim: Property patterns match nested fields.
  source: S2
  evidence: "test that an expression's properties or fields match nested patterns"
- id: C4
  claim: Logical patterns combine patterns.
  source: S2
  evidence: "test that an expression matches a logical combination of patterns"

## Sources
- [S1] Pattern matching overview - C#. Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
- [S2] Patterns - Pattern matching using the is and switch expressions. - C# reference. Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
