List patterns help you check and work with lists in a simple and clear way. They make your code easier to read and understand.
List patterns in C Sharp (C#)
public class ListPatternExample { public static void CheckListPattern(List<int> numbers) { switch (numbers) { case []: Console.WriteLine("The list is empty."); break; case [var first]: Console.WriteLine($"The list has one element: {first}"); break; case [var first, var second]: Console.WriteLine($"The list has two elements: {first} and {second}"); break; case [var first, .. var rest]: Console.WriteLine($"The list starts with {first} and has {rest.Count} more elements."); break; default: Console.WriteLine("The list has some elements."); break; } } }
List patterns use square brackets [] to match list shapes.
Use var to capture elements from the list.
[].List<int> emptyList = new(); CheckListPattern(emptyList);
[var first].List<int> singleItemList = new() { 5 }; CheckListPattern(singleItemList);
[var first, var second].List<int> twoItemsList = new() { 3, 7 }; CheckListPattern(twoItemsList);
[var first, .. var rest].List<int> manyItemsList = new() { 1, 2, 3, 4 }; CheckListPattern(manyItemsList);
This program creates four lists with different numbers of elements. It then uses list patterns to check each list and print a message describing its shape.
using System; using System.Collections.Generic; public class ListPatternExample { public static void CheckListPattern(List<int> numbers) { switch (numbers) { case []: Console.WriteLine("The list is empty."); break; case [var first]: Console.WriteLine($"The list has one element: {first}"); break; case [var first, var second]: Console.WriteLine($"The list has two elements: {first} and {second}"); break; case [var first, .. var rest]: Console.WriteLine($"The list starts with {first} and has {rest.Count} more elements."); break; default: Console.WriteLine("The list has some elements."); break; } } public static void Main() { List<int> emptyList = new(); List<int> singleItemList = new() { 5 }; List<int> twoItemsList = new() { 3, 7 }; List<int> manyItemsList = new() { 1, 2, 3, 4 }; Console.WriteLine("Before checking patterns:"); Console.WriteLine("emptyList: []"); Console.WriteLine("singleItemList: [5]"); Console.WriteLine("twoItemsList: [3, 7]"); Console.WriteLine("manyItemsList: [1, 2, 3, 4]"); Console.WriteLine("\nChecking patterns:"); CheckListPattern(emptyList); CheckListPattern(singleItemList); CheckListPattern(twoItemsList); CheckListPattern(manyItemsList); } }
Time complexity depends on the pattern and list size, but matching is generally fast for small patterns.
Space complexity is minimal since no extra storage is needed for matching.
A common mistake is forgetting the .. operator to capture the rest of the list.
Use list patterns when you want clear and readable code to check list shapes instead of manual index checks.
List patterns let you match lists by their shape using simple syntax.
They make your code easier to read and safer by handling different list cases clearly.
Use [] for empty, [var x] for one element, and [var x, .. var rest] for more elements.