Hi,
I have defined an interface as follows:
public interface IMyInterface<out T> { }
I have marked the generic parameter T as 'out' to indicate it should be covariant (that is, may also accept instances derived from T as well as T itself).
I think I misunderstand how far the compiler infers relationships, because on doing the following:
public static IMyInterface<T> Join(IMyInterface<T> first, IMyInterface<X> second) where X : T { IMyInterface<T> joined = null; List<IMyInterface<T>> list = new List<IMyInterface<T>>();
// THIS IS THE LINE THAT STATES INVALID PARAMETERS list.Add(second); // Some processing code return joined; }
I was expecting the list.Add(second) to accept parameters of type IMyInterface<X>, because I have indicated that X derives from T and IMyInterface is covariant with respect to 'T' as indicated by the 'out' parameter.
However the code refuses to compile, saying that list.Add(second) has invalid parameters.
What have I assumed incorrectly from the behavior of covariance with respect to 'T' here?