Challenge - 5 Problems
List Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Look carefully at the order of patterns and the exact list content.
✗ Incorrect
The list exactly matches the first pattern [1, 2, 3, 4], so the output is "Exact match". The switch stops at the first matching pattern.
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Check the slice pattern and the first and last elements of the array.
✗ Incorrect
The array starts with 5 and ends with 25, so it matches the first pattern [5, .., 25].
🔧 Debug
advanced2: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" };
Attempts:
2 left
💡 Hint
List patterns support arrays and top-level code/statements in C# 11+.
✗ Incorrect
The code is valid and compiles without errors in C# 11+. List patterns work on arrays (not just List), top-level statements support switch expressions, and the [1,2] pattern is syntactically valid (though it won't match the length-3 array at runtime).
📝 Syntax
advanced2:00remaining
Which list pattern syntax is correct?
Which option shows a valid list pattern syntax in C# 12+?
Attempts:
2 left
💡 Hint
Look for correct use of slice pattern with variable capture.
✗ Incorrect
Option C correctly uses the slice pattern with variable capture: [1, 2, .. var rest]. The 'var rest' is required syntax for capturing the slice. Option C misplaces 'rest'. Options C and D omit 'var', causing syntax error; D also has type mismatch (rest is array slice, not int).
🚀 Application
expert3: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); } }
Attempts:
2 left
💡 Hint
Check each array if it has at least two elements and the third element is 5.
✗ Incorrect
The pattern [_, _, .. var tail] captures elements from index 2 in tail. With tail.Length > 0 && tail[0] == 5:
- {1,2,5,6}: tail={5,6}, yes
- {3,4,5}: tail={5}, yes
- {7,8,9}: tail={9}, no
- {0,0,5,1}: tail={5,1}, yes
Three match, so count=3.