@MartinZikmund has explained why it does not work. The resolution to this kind of problem is to either derive the generic classes from a non-generic base class or to let them implement a common interface.
public class MyOtherClassBase { }
public class MyOtherClass<T> : MyOtherClassBase { }
Then you can specify MyOtherClassBase
as generic parameter to Union
explicitly. Now, Union
will expect inputs of type IEnumerable<MyOtherClassBase>
. Lists of type List<MyOtherClass<T>>
are assignment compatible to IEnumerable<MyOtherClassBase>
.
var result = listOfComplex.Union<MyOtherClassBase>(listOfBase);
Note the out
keyword in the declaration
public interface IEnumerable<out T> : System.Collections.IEnumerable
It makes the interface covariant. See also SO question <out T> vs <T> in Generics and especially Reed Copsey's answer.