I found myself in a situation where I had to do some stuff with elements of a list and then remove them from that list :
foreach (var element in someList.Where(e => e.IsMatching))
{
// do some stuff
}
someList.RemoveAll(e => e.IsMatching);
This woks perfectly but I don't like the duplicate lambda, so I declared a Func<T, bool> x = e => e.IsMatching;
and then tried to use x
as a parameter for .Where
and .RemoveAll
but I wasn't able to use x
for .RemoveAll
because its overload only accepts Predicate<T>
.
Why isn't an implicit nor explicit cast possible since both have the same return and parameter types?
(I am not looking for a way to make the conversion, everything about it is already in this quesion)