Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a contravariant interface using the in keyword.
C Sharp (C#)
public interface IProcessor<[1] T> { void Process(T item); } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
out instead of in for contravariance.Trying to use
ref or var which are invalid here.✗ Incorrect
The
in keyword declares contravariance in generic interfaces, allowing the type parameter to be used only as input.2fill in blank
mediumComplete the code to assign a contravariant interface reference correctly.
C Sharp (C#)
IProcessor<object> objProcessor = new Processor<string>();
IProcessor<string> strProcessor = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning
new Processor directly to IProcessor which is invalid.Confusing covariance and contravariance directions.
✗ Incorrect
Because of contravariance, an
IProcessor can be assigned to IProcessor reference.3fill in blank
hardFix the error in the method parameter type to support contravariance.
C Sharp (C#)
public void Execute(IProcessor<[1]> processor) { processor.Process("test"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
string restricts the method to only that type, breaking contravariance.Choosing unrelated types like
int or dynamic without context.✗ Incorrect
Using
object allows passing processors of more derived types due to contravariance.4fill in blank
hardFill both blanks to declare a contravariant interface and implement it correctly.
C Sharp (C#)
public interface IHandler<[1] T> { void Handle(T item); } public class StringHandler : IHandler<[2]> { public void Handle(string item) { } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
out instead of in for contravariance.Mismatch between interface type parameter and class implementation.
✗ Incorrect
The interface uses
in for contravariance, and the class implements it for string type.5fill in blank
hardFill all three blanks to create a contravariant delegate and use it correctly.
C Sharp (C#)
public delegate void Processor<[1] T>(T item); Processor<[2]> stringProcessor = item => Console.WriteLine(item); Processor<[3]> objectProcessor = stringProcessor;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
out instead of in for contravariance in delegates.Assigning delegate types in the wrong direction.
✗ Incorrect
Delegates can be contravariant with
in keyword; a Processor can be assigned to Processor due to contravariance.