Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a generic method named 'PrintItem' that takes one parameter of type T.
C Sharp (C#)
public void [1]<T>(T item) {
Console.WriteLine(item);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add after the method name.
Using a different method name than 'PrintItem'.
✗ Incorrect
The method name must be 'PrintItem' as specified. The generic type parameter follows the method name.
2fill in blank
mediumComplete the code to declare a generic method 'GetDefault' that returns the default value of type T.
C Sharp (C#)
public T [1]<T>() { return default(T); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not match the requirement.
Forgetting to specify the generic type parameter .
✗ Incorrect
The method name must be 'GetDefault' to match the requirement and return the default value of type T.
3fill in blank
hardFix the error in the generic method declaration by completing the method name correctly.
C Sharp (C#)
public void [1]<T>(T value) {
Console.WriteLine(value);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that is incomplete or misspelled.
Omitting the generic type parameter .
✗ Incorrect
The correct method name is 'PrintValue' to match the intended usage and avoid errors.
4fill in blank
hardFill both blanks to declare a generic method 'Swap' that swaps two variables of type T using ref parameters.
C Sharp (C#)
public void [1]<T>(ref T a, ref T [2]) { T temp = a; a = [2]; [2] = temp; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names.
Using a method name that does not describe swapping.
✗ Incorrect
The method name is 'Swap' and the second parameter name is 'b' to correctly swap two variables.
5fill in blank
hardFill all three blanks to declare a generic method 'CreatePair' that returns a Tuple of two generic types T1 and T2.
C Sharp (C#)
public Tuple<[1], [2]> [3]<T1, T2>(T1 first, T2 second) { return Tuple.Create(first, second); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up generic type parameters.
Using a method name that does not match the tuple creation.
✗ Incorrect
The generic types are T1 and T2, and the method name is 'CreatePair' to return a tuple of two values.