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

Why generics are needed in C Sharp (C#) - Test Your Understanding

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

Complete the code to declare a list that can store integers.

C Sharp (C#)
List<[1]> numbers = new List<[1]>();
Drag options to blanks, or click blank then click option'
Aint
Bstring
Cdouble
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using string instead of int for numbers.
Forgetting to specify the type inside the angle brackets.
2fill in blank
medium

Complete the code to create a generic method that returns the default value of any type.

C Sharp (C#)
public [1] GetDefaultValue<T>() { return default(T); }
Drag options to blanks, or click blank then click option'
Aobject
Bvoid
Cint
DT
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as return type when the method returns a value.
Using a specific type like int instead of the generic type.
3fill in blank
hard

Fix the error in the generic class declaration.

C Sharp (C#)
public class Box<[1]> { public T Value; }
Drag options to blanks, or click blank then click option'
Avar
BType
CT
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a keyword like var as a type parameter.
Using a lowercase name that does not match the usage.
4fill in blank
hard

Fill both blanks to create a generic method that swaps two variables.

C Sharp (C#)
public void Swap<[1]>(ref [2] a, ref [2] b) { [2] temp = a; a = b; b = temp; }
Drag options to blanks, or click blank then click option'
AT
Bint
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using different types for the type parameter and variables.
Using a specific type like int instead of the generic type.
5fill in blank
hard

Fill all three blanks to create a generic dictionary comprehension that filters entries with values greater than zero.

C Sharp (C#)
var filtered = [1].Where(kv => kv.Value [2] 0).ToDictionary(kv => kv.[3], kv => kv.Value);
Drag options to blanks, or click blank then click option'
Adata
B>
CKey
Ditems
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong collection name.
Using the wrong comparison operator.
Accessing the key incorrectly.