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

Default keyword for generic types 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 assign the default value of type T to the variable.

C Sharp (C#)
T value = [1];
Drag options to blanks, or click blank then click option'
Anew
Bdefault
Cnull
Dtypeof
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' instead of 'default' for generic types.
Using 'null' which only works for reference types.
2fill in blank
medium

Complete the method to return the default value of generic type T.

C Sharp (C#)
public T GetDefaultValue<T>() {
    return [1];
}
Drag options to blanks, or click blank then click option'
Anull
Bnew T()
Ctypeof(T)
Ddefault(T)
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use 'new T()' which requires a new() constraint.
Returning 'null' which is invalid for value types.
3fill in blank
hard

Fix the error in the code to correctly assign the default value of T.

C Sharp (C#)
T result = [1](T);
Drag options to blanks, or click blank then click option'
Adefault
Bnew
Ctypeof
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new(T)' which is invalid syntax.
Using 'typeof(T)' which returns a Type object, not a value.
4fill in blank
hard

Fill both blanks to create a method that returns the default value of T using expression-bodied syntax.

C Sharp (C#)
public T GetDefault<T>() => [1]([2]);
Drag options to blanks, or click blank then click option'
Adefault
BT
Cnew
Dtypeof
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the blanks or using 'new' instead of 'default'.
Using 'typeof' which returns a Type object.
5fill in blank
hard

Fill all three blanks to create a generic method that returns the default value of T and prints its type.

C Sharp (C#)
public T GetAndPrintDefault<T>() {
    T value = [1]([2]);
    Console.WriteLine(typeof([3]));
    return value;
}
Drag options to blanks, or click blank then click option'
Adefault
BT
Cnew
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' instead of 'default' for the value.
Using 'object' instead of 'T' in typeof.