Challenge - 5 Problems
Generic Default Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of default keyword with generic type parameter
What is the output of this C# code snippet?
C Sharp (C#)
using System; class Program { static void ShowDefault<T>() { T value = default; Console.WriteLine(value == null ? "null" : value.ToString()); } static void Main() { ShowDefault<int>(); ShowDefault<string>(); } }
Attempts:
2 left
💡 Hint
Consider what default returns for value types and reference types.
✗ Incorrect
The default keyword returns the default value for the generic type T. For int (a value type), it returns 0. For string (a reference type), it returns null.
❓ Predict Output
intermediate2:00remaining
Default keyword with nullable generic type
What will be the output of this C# program?
C Sharp (C#)
using System; class Program { static void ShowDefault<T>() { T value = default; Console.WriteLine(value == null ? "null" : value.ToString()); } static void Main() { ShowDefault<int?>(); ShowDefault<double>(); } }
Attempts:
2 left
💡 Hint
Nullable types are structs that can hold null.
✗ Incorrect
int? is a nullable int, so its default is null. double is a value type, so default is 0.
❓ Predict Output
advanced2:00remaining
Default keyword with struct generic type
What is the output of this C# code?
C Sharp (C#)
using System; struct Point { public int X, Y; public override string ToString() => $"({X},{Y})"; } class Program { static void ShowDefault<T>() where T : struct { T value = default; Console.WriteLine(value.ToString()); } static void Main() { ShowDefault<Point>(); } }
Attempts:
2 left
💡 Hint
Default of a struct initializes all fields to zero.
✗ Incorrect
The default value of a struct sets all fields to zero. So Point has X=0 and Y=0, printing (0,0).
❓ Predict Output
advanced2:00remaining
Default keyword with generic class and null check
What will this program print?
C Sharp (C#)
using System; class Container<T> { public T Value = default; public bool IsNull() => Value == null; } class Program { static void Main() { var c1 = new Container<string>(); var c2 = new Container<int>(); Console.WriteLine(c1.IsNull()); Console.WriteLine(c2.IsNull()); } }
Attempts:
2 left
💡 Hint
Value types cannot be null, reference types can.
✗ Incorrect
string is a reference type, so default is null and IsNull() returns true. int is a value type, default is 0, so IsNull() returns false.
🧠 Conceptual
expert2:00remaining
Behavior of default keyword with unconstrained generic types
Consider a generic method
void Test() with no constraints on T. Which statement about default(T) is true?Attempts:
2 left
💡 Hint
Think about how default behaves for value and reference types.
✗ Incorrect
default(T) returns the default value for value types (like 0 for int) and null for reference types. It works even if T is unconstrained.