What does the default keyword do for generic types in C#?
The default keyword returns the default value for a generic type parameter. For reference types, it returns null. For value types, it returns zero or equivalent (like 0 for numbers, false for bool).
Click to reveal answer
beginner
How do you use default to initialize a generic variable T?
You write T variable = default; or T variable = default(T);. Both assign the default value for type T.
Click to reveal answer
intermediate
What is the default value of a generic type parameter T if T is a struct?
If T is a struct (a value type), default(T) returns a struct with all fields set to their default values (like zero for numbers, false for bool).
Click to reveal answer
intermediate
Can default be used with nullable types in generics?
Yes. For nullable types like int?, default returns null, which is the default for nullable types.
Click to reveal answer
beginner
Why is default useful in generic methods or classes?
Because generic types can be any type, default lets you safely get a default value without knowing the exact type. This avoids errors and makes code flexible.
Click to reveal answer
What does default(T) return if T is a reference type?
A0
Bnull
Cfalse
DAn empty object
✗ Incorrect
For reference types, default(T) returns null.
How can you write the default value for a generic type T in C# 7.1 or later?
A<code>default</code>
B<code>null</code>
C<code>new T()</code>
D<code>default(T)</code>
✗ Incorrect
Since C# 7.1, you can use the simplified default literal without specifying the type.
What is the default value of bool when used as a generic type parameter?
Anull
Btrue
Cfalse
D1
✗ Incorrect
The default value for bool is false.
If T is int? (nullable int), what does default(T) return?
A0
Bfalse
CAn exception
Dnull
✗ Incorrect
Nullable types default to null.
Why might you prefer default over new T() in generic code?
A<code>default</code> works even if <code>T</code> has no parameterless constructor
B<code>new T()</code> always works
C<code>default</code> creates a new object
D<code>new T()</code> returns null
✗ Incorrect
new T() requires T to have a parameterless constructor, but default does not.
Explain how the default keyword works with generic types in C#.
Think about what happens when you don't know the exact type.
You got /4 concepts.
Describe a situation where using default is better than new T() in generic programming.
Consider constructor requirements.
You got /4 concepts.
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
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.
Final Answer:
The default value of type T, like 0 for int or null for reference types -> Option B
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
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.
Final Answer:
default(T) -> Option A
Quick Check:
Use parentheses with default: default(T) [OK]
Hint: Use parentheses with default keyword for types [OK]
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.
Final Answer:
0
True -> Option C
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
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.
Final Answer:
No error; code compiles and returns default value -> Option D
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
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.
Final Answer:
public object GetDefaultIfValueType<T>() {
return typeof(T).IsValueType ? default(T) : null;
} -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.