I have 2 objects and a mutual interface
public interface IMyInterface { ... }
public class MyClass1 : IMyInterface { ... }
public class MyClass2 : IMyInterface { ... }
Then I have a function that accepts a list of IMyInterface
public void MyFunction(List<IMyInterface> myObjects) { ... }
If I were to call this function using List<MyClass1>
or List<MyClass2>
I get an error saying I cannot convert List<MyClass1>
to List<IMyInterface>
, however, if I changed my function signature to -
public void MyFunction(IEnumerable<IMyInterface> myObjects) { ... }
there is no longer an error. My question is why does this work for IEnumerable
and not a concrete List
?