Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Default keyword for generic types in C Sharp (C#) - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does the default keyword return when used with a generic type parameter T in C#?
easy
A. A new instance of type T created with the default constructor
B. The default value of type T, like 0 for int or null for reference types
C. An exception is thrown if T is a reference type
D. The string representation of type T

Solution

  1. Step 1: Understand the purpose of default keyword with generic T

    The default keyword returns the default value for any type. For value types like int, it returns 0. For reference types, it returns null. When used with a generic type parameter T, default(T) returns the default value of that type without creating an instance.
  2. Final Answer:

    The default value of type T, like 0 for int or null for reference types -> Option B
  3. Quick Check:

    default(T) = default value [OK]
Hint: Remember: default returns zero/null, not a new object [OK]
Common Mistakes:
  • Thinking default creates a new instance
  • Assuming default throws exceptions
  • Confusing default with ToString()
2. Which of the following is the correct syntax to get the default value of a generic type T inside a method?
easy
A. default(T)
B. default<T>
C. default T
D. default::T

Solution

  1. Step 1: Recall syntax for default and check options

    The correct syntax to get the default value of a type is default(TypeName). For generic types, it becomes default(T). default<T> uses invalid angle brackets. default T misses parentheses. default::T uses invalid scope operator. Only default(T) is correct.
  2. Final Answer:

    default(T) -> Option A
  3. Quick Check:

    Use parentheses with default: default(T) [OK]
Hint: Use parentheses with default keyword for types [OK]
Common Mistakes:
  • Using angle brackets instead of parentheses
  • Omitting parentheses
  • Using invalid operators like ::
3. What is the output of this C# code?
static T GetDefault<T>() {
    return default(T);
}

Console.WriteLine(GetDefault<int>());
Console.WriteLine(GetDefault<string>() == null);
medium
A. 0 False
B. null True
C. 0 True
D. null False

Solution

  1. Step 1: Trace the code execution

    For GetDefault<int>(), default(int) returns 0, so first line prints 0. For GetDefault<string>(), default(string) returns null, so null == null is true, second line prints True.
  2. Final Answer:

    0 True -> Option C
  3. Quick Check:

    default int = 0, default string = null [OK]
Hint: Value types default to zero; reference types default to null [OK]
Common Mistakes:
  • Assuming default string returns empty string
  • Confusing default int with null
  • Forgetting default returns null for references
4. Identify the error in this generic method using default keyword:
public T GetValue<T>() {
    T value = default;
    return value;
}
medium
A. Syntax error: default must be used as default(T) in this context
B. Missing return type in method signature
C. Runtime error because default cannot be assigned to generic types
D. No error; code compiles and returns default value

Solution

  1. Step 1: Validate default literal usage in generic context

    Since C# 7.1, default can be used as a literal without specifying type when the compiler can infer it from context like T value = default;. The method correctly initializes and returns the default value of T. No error.
  2. Final Answer:

    No error; code compiles and returns default value -> Option D
  3. Quick Check:

    default literal works with inferred generic type [OK]
Hint: Since C# 7.1, default literal can omit type in generics [OK]
Common Mistakes:
  • Thinking default must always have parentheses
  • Assuming runtime error for default assignment
  • Missing that default literal is valid syntax
5. You want to write a generic method that returns the default value of T but only if T is a value type. Otherwise, it should return null. Which code snippet correctly implements this?
hard
A. public object GetDefaultIfValueType<T>() { return typeof(T).IsValueType ? default(T) : null; }
B. public T GetDefaultIfValueType<T>() { return default(T) ?? null; }
C. public T? GetDefaultIfValueType<T>() where T : struct { return default(T); }
D. public T GetDefaultIfValueType<T>() { if (default(T) == null) return null; else return default(T); }

Solution

  1. Step 1: Evaluate options against requirement for value/reference types

    Needs runtime check as constraints can't handle both; return object. C: typeof(T).IsValueType ? default(T) : null works. A restricts to struct; B ?? null invalid for values; D default(T) == null compile error unconstrained.
  2. Final Answer:

    public object GetDefaultIfValueType<T>() { return typeof(T).IsValueType ? default(T) : null; } -> Option A
  3. Quick Check:

    Use typeof(T).IsValueType to decide default or null [OK]
Hint: Use typeof(T).IsValueType to check type at runtime [OK]
Common Mistakes:
  • Restricting generic type with where struct and missing reference types
  • Using null-coalescing on value types
  • Comparing default(T) to null directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard