Challenge - 5 Problems
Covariance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of covariance with interface using out keyword
What is the output of this C# code using covariance with the
out keyword in interfaces?C Sharp (C#)
interface IProducer<out T> { T Produce(); } class Animal { public override string ToString() => "Animal"; } class Dog : Animal { public override string ToString() => "Dog"; } class DogProducer : IProducer<Dog> { public Dog Produce() => new Dog(); } class Program { static void Main() { IProducer<Dog> dogProducer = new DogProducer(); IProducer<Animal> animalProducer = dogProducer; // Covariance allows this Animal animal = animalProducer.Produce(); System.Console.WriteLine(animal); } }
Attempts:
2 left
💡 Hint
Remember that covariance with
out allows assignment from more derived to less derived types.✗ Incorrect
The interface
IProducer<out T> is covariant in T. This means you can assign IProducer<Dog> to IProducer<Animal>. The Produce() method returns a Dog instance, which is an Animal. So the output is Dog.❓ Predict Output
intermediate2:00remaining
Covariance with delegates and out keyword
What will this C# program print when using covariance with delegates and the
out keyword?C Sharp (C#)
delegate T Factory<out T>(); class Fruit { public override string ToString() => "Fruit"; } class Apple : Fruit { public override string ToString() => "Apple"; } class Program { static Apple CreateApple() => new Apple(); static void Main() { Factory<Apple> appleFactory = CreateApple; Factory<Fruit> fruitFactory = appleFactory; // Covariance System.Console.WriteLine(fruitFactory()); } }
Attempts:
2 left
💡 Hint
Delegates with
out type parameters support covariance.✗ Incorrect
The delegate
Factory<out T> is covariant in T. Assigning Factory<Apple> to Factory<Fruit> is allowed. The method returns an Apple, so the output is Apple.🔧 Debug
advanced2:30remaining
Why does this covariance assignment fail?
Consider this code snippet. Why does the assignment cause a compilation error despite using
out keyword?C Sharp (C#)
interface IContainer<out T> { T GetItem(); void AddItem(T item); } class Container<T> : IContainer<T> { private T item; public T GetItem() => item; public void AddItem(T item) { this.item = item; } } class Program { static void Main() { IContainer<string> stringContainer = new Container<string>(); IContainer<object> objectContainer = stringContainer; // Error here } }
Attempts:
2 left
💡 Hint
Check the variance rules for
out type parameters and method input parameters.✗ Incorrect
Interfaces marked with
out for a type parameter can only use that type as a return type (output). They cannot use it as a method input parameter. The method AddItem(T item) uses T as input, which is not allowed for covariance. Hence, the compiler error.📝 Syntax
advanced2:00remaining
Identify the syntax error with covariance and out keyword
Which option contains the syntax error when declaring a covariant interface with the
out keyword?C Sharp (C#)
interface IReadOnlyList<out T> { T Get(int index); void Add(T item); }
Attempts:
2 left
💡 Hint
Covariant type parameters can only be used as return types, not input parameters.
✗ Incorrect
The
out keyword means the type parameter is covariant and can only be used as output (return types). The method Add(T item) uses T as input, which is not allowed and causes a syntax error.🚀 Application
expert3:00remaining
Using covariance with LINQ and out keyword
Given the following code, what is the output when using covariance with
out keyword and LINQ?C Sharp (C#)
using System; using System.Collections.Generic; using System.Linq; interface ISource<out T> { IEnumerable<T> GetItems(); } class NumberSource : ISource<int> { public IEnumerable<int> GetItems() => new List<int> {1, 2, 3}; } class Program { static void Main() { ISource<int> intSource = new NumberSource(); ISource<object> objSource = intSource; // Covariance foreach (var item in objSource.GetItems().Select(x => x.ToString())) { Console.Write(item + " "); } } }
Attempts:
2 left
💡 Hint
Remember that
IEnumerable<out T> supports covariance and LINQ works on IEnumerable.✗ Incorrect
The interface
ISource<out T> is covariant, so ISource<int> can be assigned to ISource<object>. The GetItems() returns IEnumerable<int>, which is covariant to IEnumerable<object>. The LINQ Select converts each int to string, printing "1 2 3 ".