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

Covariance with out keyword in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an interface with covariance using the out keyword.

C Sharp (C#)
interface IProducer<[1]> { [1] Produce(); }
Drag options to blanks, or click blank then click option'
Ain T
Bout T
Cref T
DT
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' instead of 'out' for covariance.
Omitting the keyword entirely.
Using 'ref' which is not valid here.
2fill in blank
medium

Complete the code to assign a more derived type to a covariant interface variable.

C Sharp (C#)
IProducer<Animal> animalProducer = new Producer<[1]>();
Drag options to blanks, or click blank then click option'
Aobject
BAnimal
CDog
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using a base type like object which is not allowed in covariance assignment.
Using unrelated types like string.
3fill in blank
hard

Fix the error in the interface declaration to enable covariance.

C Sharp (C#)
interface IProducer<[1]> { T Produce(); }
Drag options to blanks, or click blank then click option'
AT
Bref T
Cin T
Dout T
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' which is for contravariance.
Not specifying any keyword.
4fill in blank
hard

Fill both blanks to declare a covariant interface and a method that returns the generic type.

C Sharp (C#)
interface IProducer<[1]> { [2] Produce(); }
Drag options to blanks, or click blank then click option'
Aout T
Bin T
CT
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in T' instead of 'out T'.
Making the method return void instead of T.
5fill in blank
hard

Fill all three blanks to declare a covariant interface, a method returning the generic type, and assign a derived type instance to a base type variable.

C Sharp (C#)
interface IProducer<[1]> { [2] Produce(); }

IProducer<Animal> producer = new Producer<[3]>();
Drag options to blanks, or click blank then click option'
Aout T
BT
CDog
Din T
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in T' instead of 'out T'.
Making the method return void.
Assigning a base type instead of a derived type.