Challenge - 5 Problems
Generic Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a generic class instance
What is the output of this C# program using a generic class?
C Sharp (C#)
using System; class Box<T> { public T Content { get; set; } public Box(T content) { Content = content; } public override string ToString() => $"Box contains: {Content}"; } class Program { static void Main() { var box = new Box<int>(123); Console.WriteLine(box); } }
Attempts:
2 left
💡 Hint
Look at how the generic type T is used and what value is passed to the constructor.
✗ Incorrect
The generic class Box stores a value of type T. Here, T is int, so Content is 123. The ToString method returns 'Box contains: 123'.
❓ Predict Output
intermediate2:00remaining
Generic class with multiple type parameters
What will be the output of this C# program with a generic class having two type parameters?
C Sharp (C#)
using System; class Pair<T1, T2> { public T1 First { get; set; } public T2 Second { get; set; } public Pair(T1 first, T2 second) { First = first; Second = second; } public override string ToString() => $"({First}, {Second})"; } class Program { static void Main() { var pair = new Pair<string, double>("pi", 3.14); Console.WriteLine(pair); } }
Attempts:
2 left
💡 Hint
Check the order of type parameters and constructor arguments.
✗ Incorrect
The Pair class stores two values of types T1 and T2. Here, T1 is string with value "pi", and T2 is double with value 3.14. The ToString method formats them as (pi, 3.14).
🔧 Debug
advanced2:00remaining
Identify the error in generic class declaration
What error does this C# code produce?
C Sharp (C#)
class Container<T> { public T Value; public Container(T val) { Value = val; } }
Attempts:
2 left
💡 Hint
Check punctuation carefully inside the constructor.
✗ Incorrect
The constructor is missing a semicolon after 'Value = val', causing a syntax error.
❓ Predict Output
advanced2:00remaining
Output of generic class with method using type constraint
What is the output of this C# program using a generic class with a type constraint?
C Sharp (C#)
using System; class Calculator<T> where T : struct { public T Add(T a, T b) { dynamic x = a; dynamic y = b; return x + y; } } class Program { static void Main() { var calc = new Calculator<int>(); Console.WriteLine(calc.Add(5, 7)); } }
Attempts:
2 left
💡 Hint
The Add method uses dynamic to add two struct values.
✗ Incorrect
The generic class restricts T to value types (struct). Using dynamic allows adding the two integers 5 and 7, resulting in 12.
🧠 Conceptual
expert2:00remaining
Understanding generic class variance
Which statement about generic class variance in C# is correct?
Attempts:
2 left
💡 Hint
Think about where 'in' and 'out' keywords are allowed in C# generics.
✗ Incorrect
In C#, only interfaces and delegates can declare variance using 'in' (contravariance) and 'out' (covariance) keywords. Generic classes do not support variance on their type parameters.