Challenge - 5 Problems
Contravariance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of contravariant interface assignment
What is the output of this C# program using contravariance with the
in keyword?C Sharp (C#)
using System; interface IContravariant<in T> { void Show(T item); } class Printer : IContravariant<object> { public void Show(object item) => Console.WriteLine(item); } class Program { static void Main() { IContravariant<string> printer = new Printer(); printer.Show("Hello World"); } }
Attempts:
2 left
💡 Hint
Remember that contravariance allows assignment from a less derived to a more derived generic type when using the
in keyword.✗ Incorrect
The interface IContravariant is contravariant in T because of the
in keyword. This means you can assign an IContravariant❓ Predict Output
intermediate2:00remaining
Contravariance with delegate parameters
What will be the output of this C# code demonstrating contravariance with delegates?
C Sharp (C#)
using System; class Animal { public override string ToString() => "Animal"; } class Dog : Animal { public override string ToString() => "Dog"; } class Program { delegate void AnimalHandler<in T>(T a); static void HandleAnimal(Animal a) => Console.WriteLine(a); static void Main() { AnimalHandler<Animal> handler = HandleAnimal; AnimalHandler<Dog> dogHandler = handler; dogHandler(new Dog()); } }
Attempts:
2 left
💡 Hint
Delegates with contravariant parameters allow assignment from a delegate with a parameter type less derived to one with a more derived parameter type.
✗ Incorrect
The delegate AnimalHandler uses
in Animal parameter, making it contravariant. The method HandleAnimal accepts Animal, so it can be assigned to a delegate expecting Dog parameter contravariantly. The output is 'Dog' because the Dog's ToString() is called.🔧 Debug
advanced2:00remaining
Identify the error in contravariant interface usage
This code tries to assign an interface with contravariance but fails. What is the error?
C Sharp (C#)
interface IProcessor<in T> { void Process(T item); } class StringProcessor : IProcessor<string> { public void Process(string item) { } } class Program { static void Main() { IProcessor<object> processor = new StringProcessor(); } }
Attempts:
2 left
💡 Hint
Contravariance allows assignment from less derived to more derived generic types, not the other way around.
✗ Incorrect
The interface is contravariant in T, so IProcessor
📝 Syntax
advanced2:00remaining
Identify the syntax error with contravariant interface
Which option correctly declares a contravariant generic interface in C#?
Attempts:
2 left
💡 Hint
Contravariant type parameters can only be used as input parameters, not as return types.
✗ Incorrect
Option A correctly declares a contravariant interface with
in T and uses T only as a method input parameter. Option A uses out T but uses T as input, which is invalid. Option A uses in T but returns T, which is not allowed. Option A uses out T but also takes T as input parameter, which is invalid.🚀 Application
expert3:00remaining
Contravariance in event handler design
You want to design an event handler interface that can accept handlers for base event types and assign them to handlers for derived event types using contravariance. Which interface declaration achieves this?
Attempts:
2 left
💡 Hint
Think about which variance keyword allows assignment from handlers of base types to handlers of derived types and how the type parameter is used.
✗ Incorrect
Option D uses to IEventHandler. The method takes TEvent as input parameter only, which is valid. Option D uses
in TEvent making the interface contravariant. This allows assigning IEventHandlerout TEvent but returns TEvent, which is not suitable for event handlers that consume events. Option D has no variance, so no assignment flexibility. Option D uses in TEvent but returns TEvent, which is invalid.