IAnimal with a method T GetAnimal().Animal and Dog, where Dog inherits from Animal.IAnimal<T> in a class called AnimalShelter<T>.IAnimal<Dog> to IAnimal<Animal>.GetAnimal() method.IAnimal with a method T GetAnimal().Animal and Dog, where Dog inherits from Animal.IAnimal<T> in a class called AnimalShelter<T>.IAnimal<Dog> to IAnimal<Animal>.GetAnimal() method.Animal with a public string property Name. Then create a class called Dog that inherits from Animal.Use public class Animal and add a property Name with getter and setter. Then create public class Dog : Animal.
IAnimal<out T> with a method T GetAnimal().Remember to use the out keyword before T in the interface declaration to enable covariance.
AnimalShelter<T> that implements IAnimal<T>. Add a private field animal of type T and a constructor that takes a T parameter to set this field. Implement the GetAnimal() method to return the stored animal.Implement the interface method and store the animal in a private field set by the constructor.
AnimalShelter<Dog> with a Dog named "Buddy". Then assign it to a variable of type IAnimal<Animal>. Use GetAnimal() and print the type name of the returned animal using Console.WriteLine.Assign dogShelter to IAnimal<Animal> to show covariance. Then print the type name of the animal returned.
What does the out keyword do when used in a generic interface in C#?
out keywordout keyword marks a generic type parameter as covariant, enabling this safe assignment.out keyword [OK]out means output, so covariance applies [OK]out restricts to value typesout allows input parametersWhich of the following is the correct syntax to declare a covariant generic interface named IReadOnlyList with type parameter T?
?
out keyword before the generic type parameter in the interface declaration.T GetItem(int index) is valid, but void Add(T item) is not.out T with output methods [OK]out and only return T, no input params [OK]in instead of out for covarianceout keywordConsider the following code snippet:
interface IProducer { T Produce(); }
class Animal { }
class Dog : Animal { }
IProducer<Dog> dogProducer = new DogProducer();
IProducer<Animal> animalProducer = dogProducer;
Animal animal = animalProducer.Produce();What is the output or result of this code?
outIProducer<out T> is covariant, so IProducer<Dog> can be assigned to IProducer<Animal>.Produce() returns a Dog instance, which is safely assigned to an Animal variable.Given the interface declaration interface IConsumer { void Consume(T item); }, what is the problem with this code?
outout keyword marks a type parameter as covariant, which means it can only be used in output positions (return types), not input parameters.void Consume(T item) uses T as an input parameter, which is not allowed for covariant type parameters.out cannot be used with input parameters like T item. -> Option Cout forbids input parameters [OK]out types cannot appear as method inputs [OK]out with input parametersout with in keywordYou have the following interfaces and classes:
interface ISource { T Get(); }
interface ITarget { void Set(T item); }
class Fruit { }
class Apple : Fruit { }
ISource<Apple> appleSource = new AppleSource();
ITarget<Fruit> fruitTarget = new FruitTarget();Which of the following assignments is valid and why?
ISource<out T> is covariant, allowing assignment from ISource<Derived> to ISource<Base>. ITarget<in T> is contravariant, allowing assignment from ITarget<Base> to ITarget<Derived>.ISource<Fruit> to ISource<Apple> is valid due to contravariance. reverses covariance (base to derived invalid). Assigning ITarget<Fruit> to ITarget<Apple> is valid due to covariance.: valid contravariant assignment but wrong reason (not covariance). Assigning ISource<Apple> to ISource<Fruit> is valid due to covariance.: valid covariant (derived Apple to base Fruit). Assigning ITarget<Apple> to ITarget<Fruit> is valid due to contravariance. reverses contravariance (derived to base invalid).Pass the test and this pattern moves unknown