The default keyword helps you get the default value of any type, especially useful when working with generic types where you don't know the exact type in advance.
Default keyword for generic types in C Sharp (C#)
default(T)
T is the generic type parameter.
For reference types, default(T) is null. For value types like int, it is 0. For bool, it is false.
int, which is 0.int defaultInt = default(int);
string, which is null.string defaultString = default(string);
T.T defaultValue = default(T);
This program defines a generic method ShowDefault that prints the default value of any type T. It shows how default works for different types.
using System; class Program { static void ShowDefault<T>() { T value = default(T); Console.WriteLine($"Default value of {typeof(T)} is: {(value == null ? "null" : value)}"); } static void Main() { ShowDefault<int>(); ShowDefault<bool>(); ShowDefault<string>(); ShowDefault<DateTime>(); } }
Use default to avoid errors when you don't know the type in advance.
For nullable types, default is null.
Since C# 7.1, you can also use default literal without specifying the type when the compiler can infer it.
default gives the default value for any type, useful in generic code.
For value types, default is usually zero or equivalent; for reference types, it is null.
It helps write safe and flexible generic methods and classes.
Practice
default keyword return when used with a generic type parameter T in C#?Solution
Step 1: Understand the purpose of default keyword with generic T
Thedefaultkeyword returns the default value for any type. For value types likeint, it returns 0. For reference types, it returnsnull. When used with a generic type parameterT,default(T)returns the default value of that type without creating an instance.Final Answer:
The default value of typeT, like 0 for int or null for reference types -> Option BQuick Check:
default(T)= default value [OK]
- Thinking default creates a new instance
- Assuming default throws exceptions
- Confusing default with ToString()
T inside a method?Solution
Step 1: Recall syntax for default and check options
The correct syntax to get the default value of a type isdefault(TypeName). For generic types, it becomesdefault(T).default<T>uses invalid angle brackets.default Tmisses parentheses.default::Tuses invalid scope operator. Onlydefault(T)is correct.Final Answer:
default(T) -> Option AQuick Check:
Use parentheses with default: default(T) [OK]
- Using angle brackets instead of parentheses
- Omitting parentheses
- Using invalid operators like ::
static T GetDefault<T>() {
return default(T);
}
Console.WriteLine(GetDefault<int>());
Console.WriteLine(GetDefault<string>() == null);
Solution
Step 1: Trace the code execution
ForGetDefault<int>(),default(int)returns 0, so first line prints 0. ForGetDefault<string>(),default(string)returnsnull, sonull == nullistrue, second line prints True.Final Answer:
0 True -> Option CQuick Check:
default int = 0, default string = null [OK]
- Assuming default string returns empty string
- Confusing default int with null
- Forgetting default returns null for references
default keyword:public T GetValue<T>() {
T value = default;
return value;
}Solution
Step 1: Validate default literal usage in generic context
Since C# 7.1,defaultcan be used as a literal without specifying type when the compiler can infer it from context likeT value = default;. The method correctly initializes and returns the default value ofT. No error.Final Answer:
No error; code compiles and returns default value -> Option DQuick Check:
default literal works with inferred generic type [OK]
- Thinking default must always have parentheses
- Assuming runtime error for default assignment
- Missing that default literal is valid syntax
T but only if T is a value type. Otherwise, it should return null. Which code snippet correctly implements this?Solution
Step 1: Evaluate options against requirement for value/reference types
Needs runtime check as constraints can't handle both; returnobject. C:typeof(T).IsValueType ? default(T) : nullworks. A restricts tostruct; B?? nullinvalid for values; Ddefault(T) == nullcompile error unconstrained.Final Answer:
public object GetDefaultIfValueType<T>() { return typeof(T).IsValueType ? default(T) : null; } -> Option AQuick Check:
Use typeof(T).IsValueType to decide default or null [OK]
- Restricting generic type with where struct and missing reference types
- Using null-coalescing on value types
- Comparing default(T) to null directly
Ready to prove it?
Pass the test and this pattern moves unknown
