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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a generic method call
What is the output of this C# program that uses a generic method to print the type and value?
C Sharp (C#)
using System;
class Program {
    static void PrintTypeAndValue<T>(T value) {
        Console.WriteLine($"Type: {typeof(T)}, Value: {value}");
    }
    static void Main() {
        PrintTypeAndValue(123);
        PrintTypeAndValue("hello");
    }
}
AType: System.Object, Value: 123\nType: System.Object, Value: hello
BType: T, Value: 123\nType: T, Value: hello
CCompilation error due to missing generic constraints
DType: System.Int32, Value: 123\nType: System.String, Value: hello
Attempts:
2 left
💡 Hint
Remember that the generic type T is inferred from the argument type.
🧠 Conceptual
intermediate
1:30remaining
Understanding generic method constraints
Which statement about generic method constraints in C# is true?
AGeneric method constraints can only specify that T must be a value type.
BYou can constrain a generic method type parameter to be a reference type using 'where T : class'.
CConstraints cannot be combined; you can only use one constraint per generic parameter.
DYou must always specify a constraint for every generic method parameter.
Attempts:
2 left
💡 Hint
Think about how you restrict types to reference types or value types.
Predict Output
advanced
2:00remaining
Output of generic method with multiple type parameters
What is the output of this C# program using a generic method with two type parameters?
C Sharp (C#)
using System;
class Program {
    static void Swap<T, U>(ref T a, ref U b) {
        Console.WriteLine($"Before swap: a={a}, b={b}");
        var temp = a;
        a = (T)(object)b;
        b = (U)(object)temp;
        Console.WriteLine($"After swap: a={a}, b={b}");
    }
    static void Main() {
        int x = 5;
        string y = "10";
        Swap(ref x, ref y);
    }
}
ARuntime InvalidCastException
BCompilation error due to invalid cast
CBefore swap: a=5, b=10\nAfter swap: a=5, b=10
DBefore swap: a=5, b=10\nAfter swap: a=10, b=5
Attempts:
2 left
💡 Hint
Casting between unrelated types using (T)(object) can cause runtime errors.
🔧 Debug
advanced
1:30remaining
Identify the error in generic method declaration
What error does this C# code produce?
C Sharp (C#)
class Program {
    static void PrintValue<T>() {
        Console.WriteLine(value);
    }
    static void Main() {
        PrintValue<int>();
    }
}
ARuntime error: NullReferenceException
BCompilation error: Missing generic type constraint
CCompilation error: 'value' does not exist in the current context
DNo error, prints default value
Attempts:
2 left
💡 Hint
Check if the variable 'value' is declared or passed.
🧠 Conceptual
expert
2:00remaining
Generic method type inference behavior
Given the generic method declaration
void Display(T item) { Console.WriteLine(item); }

and the call
Display(null);

What happens when you compile this code in C#?
ACompilation error: Cannot infer type for T because null is ambiguous
BCompiles and prints an empty line
CCompiles and prints 'null'
DRuntime error: NullReferenceException
Attempts:
2 left
💡 Hint
Think about how the compiler infers generic types from null arguments.