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

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

Choose your learning style9 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.