Recall & Review
beginner
What is a list pattern in C#?
A list pattern in C# is a way to match elements inside a list or array by their order and values. It lets you check if a list has specific elements in a certain sequence.
Click to reveal answer
beginner
How do you match the first two elements of a list using list patterns?
You write a pattern like
[var first, var second, ..]. This matches a list where the first element is stored in first, the second in second, and .. means any remaining elements.Click to reveal answer
beginner
What does the
.. symbol mean in a list pattern?The
.. symbol means 'match the rest of the list'. It allows the pattern to match lists longer than the elements explicitly listed.Click to reveal answer
intermediate
Can list patterns be used to check for an exact list length?
Yes. If you write a pattern without
.., like [1, 2, 3], it matches only lists with exactly three elements: 1, 2, and 3 in order.Click to reveal answer
intermediate
Give an example of using list patterns in a
switch expression.Example:<br>
return list switch {
[1, 2, 3] => "Exact match",
[1, 2, ..] => "Starts with 1, 2",
_ => "Other"
};This checks the list and returns a string based on the pattern matched.Click to reveal answer
What does the list pattern
[var x, var y, ..] match?✗ Incorrect
The pattern matches lists with two or more elements, capturing the first two into variables x and y, and the rest is ignored by '..'.
Which list pattern matches only a list with exactly three elements 1, 2, and 3?
✗ Incorrect
The pattern [1, 2, 3] matches only lists with exactly those three elements in order.
In C# list patterns, what does the underscore (_) pattern mean?
✗ Incorrect
The underscore (_) is a discard pattern that matches anything, including any list.
Which of these is a valid list pattern in C#?
✗ Incorrect
List patterns use square brackets [] to match elements in order.
What happens if you use
[1, 2, ..] to match the list [1, 2]?✗ Incorrect
The pattern matches lists with two or more elements starting with 1 and 2. The list [1, 2] matches because '..' can match zero or more elements.
Explain how list patterns work in C# and how you can use them to check list contents.
Think about how you check items in a list one by one.
You got /4 concepts.
Describe a scenario where list patterns can simplify your code compared to traditional if-else checks.
Imagine you want to do different things based on the first few items in a list.
You got /4 concepts.