This is because T
parameter in List<T>
is not covariant. But it is in IEnumerable<out T>
:
out T
The type of objects to enumerate.
This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived.
So you can do this, if you'd consider to change signature of getItems
:
public IEnumerable<IMyInterface> getItems(int id)
{
return new List<MyClass>() as IEnumerable<IMyInterface>;
}
You can find more information about covariance and contravariance here.