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

Generic method declaration 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 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'
APrintItem
BDisplay
CShowItem
DPrint
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add after the method name.
Using a different method name than 'PrintItem'.
2fill in blank
medium

Complete 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'
AGetDefault
BDefaultValue
CGetValue
DFetchDefault
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not match the requirement.
Forgetting to specify the generic type parameter .
3fill in blank
hard

Fix 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'
APrintVal
BPrintValue
CPrint
DDisplayValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that is incomplete or misspelled.
Omitting the generic type parameter .
4fill in blank
hard

Fill 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'
ASwap
BSwapValues
Cb
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names.
Using a method name that does not describe swapping.
5fill in blank
hard

Fill 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'
AT1
BT2
CCreatePair
DMakePair
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up generic type parameters.
Using a method name that does not match the tuple creation.