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

Default keyword for generic types in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Default Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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>();
    }
}
ACompilation error
B
null
null
C
0
""
D
0
null
Attempts:
2 left
💡 Hint
Consider what default returns for value types and reference types.
Predict Output
intermediate
2: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>();
    }
}
A
null
0
B
0
0
C
null
null
DCompilation error
Attempts:
2 left
💡 Hint
Nullable types are structs that can hold null.
Predict Output
advanced
2: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>();
    }
}
Anull
B(,)
C(0,0)
DCompilation error
Attempts:
2 left
💡 Hint
Default of a struct initializes all fields to zero.
Predict Output
advanced
2: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());
    }
}
A
True
False
B
False
False
C
True
True
DCompilation error
Attempts:
2 left
💡 Hint
Value types cannot be null, reference types can.
🧠 Conceptual
expert
2: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?
AIt returns null for all types, including value types.
BIt returns the default value for value types and null for reference types.
CIt causes a compile-time error if <code>T</code> is unconstrained.
DIt returns zero for numeric types and null for reference types.
Attempts:
2 left
💡 Hint
Think about how default behaves for value and reference types.