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

Dictionary key-value collection 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 create a new dictionary in C#.

C Sharp (C#)
var dict = new Dictionary<string, int>[1];
Drag options to blanks, or click blank then click option'
A{}
B<>
C[]
D()
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} instead of parentheses () to instantiate the dictionary.
Using angle brackets <> which are for generic type parameters, not instantiation.
2fill in blank
medium

Complete the code to add a key-value pair to the dictionary.

C Sharp (C#)
dict.[1]("apple", 5);
Drag options to blanks, or click blank then click option'
AInsert
BPut
CAdd
DSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using Insert or Put which are not dictionary methods in C#.
Using Set which is not a method for adding items to a dictionary.
3fill in blank
hard

Fix the error in accessing a value by key from the dictionary.

C Sharp (C#)
int count = dict[[1]];
Drag options to blanks, or click blank then click option'
Acount
B"apple"
Capple
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using the key without quotes which causes a compile error.
Using a number or variable name instead of the string key.
4fill in blank
hard

Fill both blanks to check if a key exists and get its value safely.

C Sharp (C#)
if (dict.[1]("banana", out int [2])) {
    Console.WriteLine([2]);
}
Drag options to blanks, or click blank then click option'
ATryGetValue
BContainsKey
Cvalue
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Using ContainsKey which only checks existence but does not get the value.
Using variable names that are not declared or mismatched.
5fill in blank
hard

Fill all three blanks to create a dictionary from a list of keys with initial value 0.

C Sharp (C#)
var keys = new List<string> {"a", "b", "c"};
var dict = keys.ToDictionary([1] => [2], [3] => 0);
Drag options to blanks, or click blank then click option'
Akey
Bk
C_
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the two lambdas which causes errors.
Using underscore '_' as a variable name in this context.