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

List patterns in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the list starts with 1, 2, 3 using list pattern.

C Sharp (C#)
int[] numbers = {1, 2, 3, 4, 5};
if (numbers is [1]) {
    Console.WriteLine("Starts with 1, 2, 3");
}
Drag options to blanks, or click blank then click option'
A[1, 2, 3, ..]
B[1, 2, 3]
C[.., 1, 2, 3]
D[1, .., 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using exact length pattern without .. causes mismatch if list is longer.
Placing .. in the wrong position.
2fill in blank
medium

Complete the code to check if the list has exactly two elements 5 and 10.

C Sharp (C#)
int[] values = {5, 10};
if (values is [1]) {
    Console.WriteLine("Exactly 5 and 10");
}
Drag options to blanks, or click blank then click option'
A[5, 10]
B[5, ..]
C[.., 10]
D[5]
Attempts:
3 left
💡 Hint
Common Mistakes
Using .. which allows extra elements.
Using a single element pattern.
3fill in blank
hard

Fix the error in the pattern to check if the list starts with 0 and ends with 9.

C Sharp (C#)
int[] arr = {0, 1, 2, 3, 9};
if (arr is [1]) {
    Console.WriteLine("Starts with 0 and ends with 9");
}
Drag options to blanks, or click blank then click option'
A[0, 9]
B[0, .., 9]
C[.., 0, 9]
D[0, ..]
Attempts:
3 left
💡 Hint
Common Mistakes
Using pattern without .. which matches only two elements.
Placing .. incorrectly.
4fill in blank
hard

Fill both blanks to match a list starting with 7 and having any elements after.

C Sharp (C#)
int[] data = {7, 8, 9};
if (data is [1]) {
    Console.WriteLine("Starts with 7");
}

if (data is [2]) {
    Console.WriteLine("Starts with 7 and 8");
}
Drag options to blanks, or click blank then click option'
A[7, ..]
B[7]
C[7, 8, ..]
D[.., 7]
Attempts:
3 left
💡 Hint
Common Mistakes
Using patterns without .. which require exact length.
Confusing the order of elements.
5fill in blank
hard

Fill all three blanks to create a dictionary from a list where keys are uppercase strings and values are their lengths if length > 3.

C Sharp (C#)
var words = new List<string> {"apple", "bat", "carrot", "dog"};
var result = words.ToDictionary(
    [1] => [2].ToUpper(),
    [3] => [3].Length
).Where(kv => kv.Value > 3).ToDictionary(kv => kv.Key, kv => kv.Value);
Drag options to blanks, or click blank then click option'
Aword
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing errors.
Not converting to uppercase for keys.