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

Covariance with out keyword in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Covariance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
ARuntime exception
BAnimal
CCompilation error due to covariance
DDog
Attempts:
2 left
💡 Hint
Remember that covariance with out allows assignment from more derived to less derived types.
Predict Output
intermediate
2: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());
    }
}
AApple
BFruit
CCompilation error due to delegate variance
DNullReferenceException at runtime
Attempts:
2 left
💡 Hint
Delegates with out type parameters support covariance.
🔧 Debug
advanced
2: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
    }
}
ABecause <code>IContainer&lt;out T&gt;</code> cannot have input parameters like <code>AddItem(T item)</code>.
BBecause <code>string</code> cannot be assigned to <code>object</code>.
CBecause covariance only works with classes, not interfaces.
DBecause <code>Container&lt;T&gt;</code> does not implement <code>IContainer&lt;T&gt;</code> correctly.
Attempts:
2 left
💡 Hint
Check the variance rules for out type parameters and method input parameters.
📝 Syntax
advanced
2: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);
}
AThe method <code>Get(int index)</code> cannot return <code>T</code> in a covariant interface.
BThe interface cannot have a method <code>Add(T item)</code> because <code>T</code> is covariant (out).
CThe <code>out</code> keyword must be placed after the interface name, not before <code>T</code>.
DThe interface must be declared as <code>interface IReadOnlyList&lt;in T&gt;</code> for covariance.
Attempts:
2 left
💡 Hint
Covariant type parameters can only be used as return types, not input parameters.
🚀 Application
expert
3: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 + " ");
        }
    }
}
ACompilation error due to covariance with IEnumerable
B1 2 3 4
C1 2 3
DRuntime exception due to invalid cast
Attempts:
2 left
💡 Hint
Remember that IEnumerable<out T> supports covariance and LINQ works on IEnumerable.