Here is a piece of my code:
public interface IA
{
void Method(IB entities);
}
public interface IB { }
The designator of contravariance in C# (i.e. in
) is intuitive only at the immediate level, when you make a method that "takes in" a parameter of generic type. Internally, however, contravariance means an inversion of a relation (Q&A with an explanation) so using in
inside IA
makes it incompatible with IB
.
The problem is best illustrated with an example. Consider class Animal
and its derived class Tiger
. Let's also assume that IB
has a method void MethodB(T input)
, which is called from IA
's Method
:
class A_Impl : IA {
T data;
public void Method(IB entities) {
entities.MethodB(data);
}
}
Declaring IA
and IB
means that you can do
IA aForAnimals = new A_Impl();
IA aForTigers = aForAnimals;
IA
has a method that takes IB
, which we can call like this:
aForTigers.Method(new B_Impl());
This is a problem, because now A_Impl
passes an Animal
to MethodB
of an interface that expects a Tiger
.
You would have no problem with IB
, though - both with covariance and contravariance:
public interface IB {
// ^^^
}
// This works
public interface IA {
void Method(IB x);
}
// This works too
public interface IC {
void Method(IB x);
}