0
0
C Sharp (C#)programming~5 mins

List patterns in C Sharp (C#)

Choose your learning style9 modes available
Introduction

List patterns help you check and work with lists in a simple and clear way. They make your code easier to read and understand.

When you want to check if a list is empty or has certain elements.
When you want to get the first few items from a list easily.
When you want to write clear code that handles different list shapes.
When you want to avoid errors by safely checking list contents before using them.
When you want to match lists with specific patterns to decide what to do next.
Syntax
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.

Examples
This matches the empty list pattern [].
C Sharp (C#)
List<int> emptyList = new();
CheckListPattern(emptyList);
This matches the pattern with one element [var first].
C Sharp (C#)
List<int> singleItemList = new() { 5 };
CheckListPattern(singleItemList);
This matches the pattern with two elements [var first, var second].
C Sharp (C#)
List<int> twoItemsList = new() { 3, 7 };
CheckListPattern(twoItemsList);
This matches the pattern with first element and rest [var first, .. var rest].
C Sharp (C#)
List<int> manyItemsList = new() { 1, 2, 3, 4 };
CheckListPattern(manyItemsList);
Sample Program

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.

C Sharp (C#)
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);
    }
}
OutputSuccess
Important Notes

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.

Summary

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.