It seems to me that a lot of the extension methods on IList<T>
are just as applicable to IEnumerable<T>
- such as FindAll
and RemoveAll
.
Can anyone explain the reasoning why they are not there?
RemoveAll
makes no sense since there is no Remove
etc on that API - however there is a FindAll
from 3.5 onwards - but it is known as Where
:
IEnumerable<Foo> source = ...
var filtered = source.Where(x => x.IsActive && x.Id = 25);
which is equivalent to:
IEnumerable<Foo> source = ...
var filtered = from x in source
where x.IsActive && x.Id == 25
select x;
Enumerable does not imply there is an underlying collection, so you can't know whether there is something to remove from or not. If there is an underlying collection, you don't know whether it supports a remove operation.
Here is an example method that enumerates odd numbers. If you could "remove" 7 from enumerable, what would happen? Where would it be removed from?
public IEnumerable<int> GetOddPositiveNumbers()
{
int i = 0;
while (true)
{
yield return 2*(i++)+1;
}
}
What you might be looking for is Where
and Except
that allows you to filter the enumerable.
For RemoveAll
isn't applicable on IEnumerable
because IEnumerable is read-only.
For FindAll
, see Marc's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With