You cannot use a List one type for a List another type.
And if you think about it, you would be glad you couldn't. Imagine the chaos that you could cause if it was possible:
interface ICustomRequired { } class ImplementationOne : ICustomRequired { } class ImplementationTwo: ICustomRequired { } var listOne = new List<ImplementationOne>(); var castReference = listOne as List<ICustomRequired>();
Please note that this line of code is legal:
exampleList as IEnumerable<ICustomRequired>;
This would be safe because IEnumerable does not provide you with any means to add new objects.
IEnumerable<T> is actually defined as IEnumerable<out t> , which means that the parameter is of type Covariant .
Can you change the function parameter to IEnumerable<ICustomRequired> ?
Otherwise, the only option is to create a new list.
var newList = (exampleList as IEnumerable<ICustomRequired>).ToList();
or
var newList = exampleList.Cast<ICustomRequired>().ToList();
source share