From dc1b3d5e740c8325568db275f5a28a33561e99c5 Mon Sep 17 00:00:00 2001 From: Mark J Price Date: Fri, 18 Mar 2022 07:55:09 +0000 Subject: [PATCH] Update README.md --- docs/previews/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/previews/README.md b/docs/previews/README.md index f139941..f3e3005 100644 --- a/docs/previews/README.md +++ b/docs/previews/README.md @@ -52,7 +52,21 @@ With C# 11, the above will compile. Finally! ## List pattern matching +Any type that has an indexer, and `Length` and `Count` properties (like arrays and any type that implements `ICollection`) can use list pattern matches. +```cs +int[] numbers = { 1, 2, 3, 5, 7, 11 }; + +string message = numbers switch +{ + [ 1, 2, 3, 5, 7, 11 ] => "This is an exact match.", + [ 1, 2, _, _, 7, _ ] => "This is a wildcard match.", + [1, 2, .., 11] => "This is a slice match.", + _ => "This is NOT a match." +}; +``` + +To use slice matches, the type must have an indexer that accepts a `Range` parameter, or implements a `Slice(int, int)` method. # Chapter 8 - Working with Common .NET Types