Bird
Raised Fist0
C Sharp (C#)programming~20 mins

List patterns in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
List Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of list pattern matching with nested lists
What is the output of this C# code using list patterns?
C Sharp (C#)
using System;

class Program {
    static void Main() {
        var list = new[] {1, 2, 3, 4};
        string result = list switch {
            [1, 2, 3, 4] => "Exact match",
            [1, 2, ..] => "Starts with 1, 2",
            [.., 4] => "Ends with 4",
            _ => "No match"
        };
        Console.WriteLine(result);
    }
}
AExact match
BStarts with 1, 2
CEnds with 4
DNo match
Attempts:
2 left
💡 Hint
Look carefully at the order of patterns and the exact list content.
❓ Predict Output
intermediate
2:00remaining
Result of list pattern with slice and discard
What will be printed by this C# program?
C Sharp (C#)
using System;

class Program {
    static void Main() {
        int[] numbers = {5, 10, 15, 20, 25};
        string output = numbers switch {
            [5, .., 25] => "Starts with 5 and ends with 25",
            [5, ..] => "Starts with 5",
            _ => "No match"
        };
        Console.WriteLine(output);
    }
}
ANo match
BStarts with 5
CCompilation error
DStarts with 5 and ends with 25
Attempts:
2 left
💡 Hint
Check the slice pattern and the first and last elements of the array.
🔧 Debug
advanced
2:00remaining
Identify the error in list pattern usage
This code uses a list pattern. Which option is correct about it?
C Sharp (C#)
int[] arr = {1, 2, 3};
string res = arr switch {
    [1, 2, 3] => "Match",
    [1, 2] => "Partial",
    _ => "No match"
};
AThe pattern [1, 2] is invalid because it does not match the array length.
BThe code is valid and compiles without errors.
CList patterns require the array to be a List<T>, not an array.
DThe switch expression must be inside a method, not at top level.
Attempts:
2 left
💡 Hint
List patterns support arrays and top-level code/statements in C# 11+.
📝 Syntax
advanced
2:00remaining
Which list pattern syntax is correct?
Which option shows a valid list pattern syntax in C# 12+?
Avar result = arr switch { [1, 2, ..] rest => rest.Length, _ => 0 };
Bvar result = arr switch { [1, 2, ..rest] => rest.Length, _ => 0 };
Cvar result = arr switch { [1, 2, .. var rest] => rest.Length, _ => 0 };
Dvar result = arr switch { [1, 2, ..rest] => rest, _ => 0 };
Attempts:
2 left
💡 Hint
Look for correct use of slice pattern with variable capture.
🚀 Application
expert
3:00remaining
Count how many lists match a pattern
Given a list of integer arrays, how many match the pattern [_, _, .. var tail] where tail starts with 5?
C Sharp (C#)
using System;
using System.Linq;

class Program {
    static void Main() {
        int[][] lists = {
            new[] {1, 2, 5, 6},
            new[] {3, 4, 5},
            new[] {7, 8, 9},
            new[] {0, 0, 5, 1}
        };
        int count = lists.Count(arr => arr is [_, _, .. var tail] && tail.Length > 0 && tail[0] == 5);
        Console.WriteLine(count);
    }
}
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Check each array if it has at least two elements and the third element is 5.

Practice

(1/5)
1. What does the list pattern [] match in C#?
easy
A. An empty list with no elements
B. A list with exactly one element
C. A list with two or more elements
D. Any list regardless of elements

Solution

  1. Step 1: Understand the empty list pattern

    The pattern [] is used to match lists that have no elements at all.
  2. Step 2: Compare with other list patterns

    Patterns like [var x] match one element, and [var x, .. var rest] match lists with multiple elements, so they don't match empty lists.
  3. Final Answer:

    An empty list with no elements -> Option A
  4. Quick Check:

    Empty list = [] pattern [OK]
Hint: Empty list pattern is just empty brackets [] [OK]
Common Mistakes:
  • Confusing [] with single element pattern
  • Thinking [] matches any list
  • Assuming [] matches lists with elements
2. Which of the following is the correct syntax to match a list with exactly one element using list patterns in C#?
easy
A. [var x, .. var rest]
B. [var x]
C. [.. var rest]
D. []

Solution

  1. Step 1: Identify the pattern for one element

    The pattern [var x] matches a list with exactly one element, binding it to x.
  2. Step 2: Understand other patterns

    [var x, .. var rest] matches lists with at least one element plus the rest, [.. var rest] matches any list, and [] matches empty lists.
  3. Final Answer:

    [var x] -> Option B
  4. Quick Check:

    One element list = [var x] [OK]
Hint: One element list uses [var x] syntax [OK]
Common Mistakes:
  • Using [var x, .. var rest] for single element
  • Using [] for single element list
  • Confusing .. var rest with single element
3. What is the output of this C# code snippet using list patterns?
var list = new List<int> { 5, 10, 15 };
switch (list)
{
    case []:
        Console.WriteLine("Empty");
        break;
    case [var x]:
        Console.WriteLine($"One element: {x}");
        break;
    case [var x, .. var rest]:
        Console.WriteLine($"First: {x}, Rest count: {rest.Count}");
        break;
}
medium
A. Empty
B. One element: 5
C. No match
D. First: 5, Rest count: 2

Solution

  1. Step 1: Analyze the list contents

    The list has three elements: 5, 10, and 15.
  2. Step 2: Match the list pattern cases

    The first case matches empty list, which is false. The second case matches exactly one element, which is false. The third case matches a list with at least one element: [var x, .. var rest]. Here, x is 5, and rest contains 10 and 15, so count is 2.
  3. Final Answer:

    First: 5, Rest count: 2 -> Option D
  4. Quick Check:

    List with 3 elements matches [x, ..rest] pattern [OK]
Hint: More than one element matches [x, ..rest] pattern [OK]
Common Mistakes:
  • Choosing empty or one element case incorrectly
  • Miscounting rest elements
  • Ignoring pattern order in switch
4. Identify the error in this C# code using list patterns:
var numbers = new List<int> { 1, 2, 3 };
if (numbers is [var first, var second])
{
    Console.WriteLine($"First: {first}, Second: {second}");
}
medium
A. The variable names first and second are invalid
B. List patterns cannot be used with List<int>
C. The pattern [var first, var second] requires exactly two elements, but list has three
D. Missing else block after if

Solution

  1. Step 1: Understand the pattern [var first, var second]

    This pattern matches lists with exactly two elements, binding them to first and second.
  2. Step 2: Check the list size

    The list has three elements: 1, 2, and 3, so it does not match the pattern requiring exactly two elements.
  3. Final Answer:

    The pattern [var first, var second] requires exactly two elements, but list has three -> Option C
  4. Quick Check:

    Pattern length must match list length exactly [OK]
Hint: Pattern length must match list length exactly [OK]
Common Mistakes:
  • Assuming pattern matches partial list
  • Thinking variable names cause error
  • Ignoring list size in pattern matching
5. Given a list of integers, how can you use list patterns in C# to check if the list starts with 1 and has at least two elements? Choose the correct pattern.
hard
A. [1, var second, .. var rest]
B. [var first, 1, .. var rest]
C. [1, .. var rest]
D. [.. var rest, 1]

Solution

  1. Step 1: Understand the requirement

    The list must start with 1, then have a second element, and then zero or more elements after that (at least two elements total).
  2. Step 2: Analyze each pattern

    [1, var second, .. var rest]: [1, var second, .. var rest] matches a list starting with 1, then a second element, then the rest (zero or more). This fits the requirement.
    [var first, 1, .. var rest]: [var first, 1, .. var rest] matches lists where the second element is 1, not the first.
    [1, .. var rest]: [1, .. var rest] matches lists starting with 1 but allows zero or more elements after, so it matches lists with only one element too.
    [.. var rest, 1]: [.. var rest, 1] matches lists ending with 1, not starting.
  3. Final Answer:

    [1, var second, .. var rest] -> Option A
  4. Quick Check:

    Start with 1 and at least two elements = [1, var second, .. rest] [OK]
Hint: Start with 1 and two elements uses [1, var second, .. rest] [OK]
Common Mistakes:
  • Confusing element positions in pattern
  • Using patterns that allow only one element
  • Matching end of list instead of start
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard