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

Collection initialization syntax 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 initialize a list of integers with values 1, 2, and 3.

C Sharp (C#)
var numbers = new List<int> [1] 1, 2, 3 [2];
Drag options to blanks, or click blank then click option'
A{
B[
C}
D)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of curly braces {} for collection initialization.
Using square brackets [] which are for arrays, not collections.
2fill in blank
medium

Complete the code to initialize a dictionary with keys "apple" and "banana" and values 1 and 2.

C Sharp (C#)
var fruitCounts = new Dictionary<string, int> [1] {"apple", 1}, {"banana", 2} [2];
Drag options to blanks, or click blank then click option'
A{
B[
C}
D)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of curly braces {} for dictionary initialization.
Using square brackets [] which are not valid for dictionary initializers.
3fill in blank
hard

Fix the error in the code to correctly initialize a list of strings with "red", "green", and "blue".

C Sharp (C#)
var colors = new List<string> [1] "red", "green", "blue" [2];
Drag options to blanks, or click blank then click option'
A[
B{
C}
D)
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] which are for arrays, not collections.
Using parentheses () which are for method calls, not initializers.
4fill in blank
hard

Fill both blanks to initialize a dictionary with keys 1 and 2 and values "one" and "two".

C Sharp (C#)
var numbers = new Dictionary<int, string> [1] {1, "one"}, {2, "two"} [2];
Drag options to blanks, or click blank then click option'
A{
B[
C}
D)
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] or parentheses () instead of curly braces.
Forgetting to close the initializer with the correct brace.
5fill in blank
hard

Fill the two blanks to initialize a list of tuples with (1, "one"), (2, "two"), and (3, "three").

C Sharp (C#)
var list = new List<(int, string)> [1] (1, "one"), (2, "two"), (3, "three") [2];
Drag options to blanks, or click blank then click option'
A{
B}
C;
D)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of curly braces for the initializer.