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

Generic class declaration in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
ABox contains: System.Int32
BBox contains: 123
CBox contains: T
DCompilation error
Attempts:
2 left
💡 Hint
Look at how the generic type T is used and what value is passed to the constructor.
Predict Output
intermediate
2: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);
    }
}
A(3.14, pi)
BCompilation error
C(System.String, System.Double)
D(pi, 3.14)
Attempts:
2 left
💡 Hint
Check the order of type parameters and constructor arguments.
🔧 Debug
advanced
2: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;
    }
}
ASyntaxError: Missing semicolon after 'Value = val'
BTypeError: Cannot assign T to Value
CRuntime error: NullReferenceException
DNo error, compiles successfully
Attempts:
2 left
💡 Hint
Check punctuation carefully inside the constructor.
Predict Output
advanced
2: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));
    }
}
A12
BCompilation error due to dynamic usage
CRuntime error: InvalidCastException
D0
Attempts:
2 left
💡 Hint
The Add method uses dynamic to add two struct values.
🧠 Conceptual
expert
2:00remaining
Understanding generic class variance
Which statement about generic class variance in C# is correct?
AVariance allows changing the type parameter of a generic class at runtime.
BGeneric classes support covariance and contravariance on their type parameters by default.
COnly interfaces and delegates can declare variance with 'in' and 'out' keywords, not classes.
DVariance means a generic class can only accept reference types as type parameters.
Attempts:
2 left
💡 Hint
Think about where 'in' and 'out' keywords are allowed in C# generics.