C# Reference ยท Classes and Object-Oriented Concepts
List patterns
Reference for C# 11 list patterns: matching arrays and lists by length and position, slice patterns with .., capturing parts and the types that support them.
A list pattern matches a sequence by its length and by the elements at fixed positions. You write the elements in square brackets, and each element is itself a pattern: [1, 2, 3] matches exactly those three values, [_, _] matches any two-element list, and [var a, var b] also captures them. Inside the brackets, .. is a slice pattern that stands for any number of elements, including none.
List patterns work with arrays, List<T>, strings and any type that has a Count or Length property and an int indexer. A slice pattern additionally needs a range indexer or a Slice method if you want to capture it. They were added in C# 11.
if (numbers is [var first, .., var last]) { Console.WriteLine($"{first} to {last}"); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
expr is [pattern, pattern, ...] expr is [pattern, .., pattern] expr is [pattern, .. var name] case [pattern, ..]:
| Element | Meaning |
|---|---|
[] |
A sequence of length 0. |
[p1, p2] |
Exactly two elements; p1 matches the first and p2 the second. |
_ / var x |
Any single element, optionally captured. |
.. |
Slice pattern: zero or more elements. Allowed at most once per list pattern. |
.. var name or .. [p, q] |
Captures the slice (a new array, Span, or the result of Slice/range indexer) or matches it with another list pattern. |
Applicable types: the type needs an accessible Length or Count property returning int, and an int indexer (or implicit index support). Arrays, string, List<T>, Span<T>, ReadOnlySpan<T>, ImmutableArray<T> qualify.
Usage notes
- Length is checked first.
[var a, var b]fails for lists of any other length, without reading elements. A pattern with a slice only constrains the minimum length. IEnumerable<T>is not supported because it has no length or indexer. CallToArray()orToList()first.- A slice capture copies for arrays and strings.
.. var tailon an array allocates a new array (slicing is a copy), so recursive code likeSumabove is O(nยฒ). Use indexes or spans for large inputs. - Elements can be any pattern:
[> 0, > 0, ..],[{ Age: > 18 }, ..],["add", string item]. - Exhaustiveness is conservative. The compiler can prove
[],[var x]and[.., var last]together cover everything of a certain shape, but otherwise add_. - Index from the end is implicit:
[.., var last]reads the last element without you computingLength - 1. - Strings work because they have
Lengthand an indexer, so"level" is [var f, .., var l]is valid and each element is achar. - Nulls: a
nulllist never matches a list pattern (not even[]); testis nullseparately. - Side-effect free. The compiler may read elements in any order or more than once, so pattern code should not depend on a custom indexer's side effects.