Instead of actually looking at the code so that we can fix it, how about changing the method:
public void DoSomething<T>(IEnumerable<T> items) where T : ParentType { ... }
Or, if you use C # 4 and .NET 4, this should be good, since IEnumerable<T>
is covariant in T
in .NET 4.
public void DoSomething(IEnumerable<ParentType> items) { ... }
Do you really need a method to accept a List<ParentType>
? In the end, if you are going to call:
var parentList = childList.Cast<ParentType>().ToList();
and pass this to the method, then you have two completely separate lists at this point.
By the way, another effect of covariant IEnumerable<T>
is that in .NET 4 you can avoid calling Cast
and just call:
var parentList = childList.ToList<ParentType>();
EDIT: now that you have posted your code, the only question is not to call the Cast
method as a method:
// This... helper(listB.Cast<P>.ToList()) // should be this: helper(listB.Cast<P>().ToList())
source share