Read the syntax
Use C# 11 list and slice patterns to recognise sequence shapes without a manual index chain.
The slice in the second permits the middle element between the two boundaries.
What it is and why to use it
A parser may care that a small sequence begins with a marker or contains exactly two values. List patterns express element and length requirements together. They extend pattern matching to suitable indexable sequences such as arrays, not to every arbitrary IEnumerable. C# 11 introduced this syntax; the examples use C# 12.
How it works
A pattern such as [1, 2, 3] matches exactly three elements with those values. A slice pattern .. matches zero or more intervening elements. A declaration such as .. var tail captures the slice where the input type supports that operation. Capturing an array slice can allocate a new array; it is not universally a zero-copy operation.
Common mistakes and limitations
Do not assume every sequence object supports list-pattern indexing. Do not assume a slice is always a view. Empty sequences cannot match a required first element, so include an appropriate failure path. A list pattern recognises shape and values, not the validity of a whole external protocol.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
if (values is [var first, .. var rest]) { /* matched head and tail */ }Examples
Compare exact and flexible shapes
Match a fixed array using an exact pattern and a boundary pattern.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
int[] values = { 1, 2, 3 };
Console.WriteLine(values is [1, 2, 3]);
Console.WriteLine(values is [1, .., 3]);
Console.WriteLine(values is [1, 3]);
- The first pattern requires three exact elements and matches.
- The slice in the second permits the middle element between the two boundaries.
- The final pattern requires exactly two elements and fails because the array has three.
- Expected output: True True False
- Use shape patterns when they are clearer than manual indexing.
Capture a head and an array tail
Use the matched variables only inside the successful branch.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
int[] values = { 4, 5, 6 };
if (values is [var head, .. var tail]) {
Console.WriteLine(head);
Console.WriteLine(string.Join(", ", tail));
}
- head receives the first integer after the shape is matched.
- tail receives the remaining array slice.
- The branch prints both parts. For this array input, the captured tail is a separate array, so do not assume this form avoids allocation.
- Expected output: 4 5, 6
- Use shape patterns when they are clearer than manual indexing.
Best practices
- Use shape patterns when they are clearer than manual indexing.
- Consider allocation when capturing slices in performance-sensitive code.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference