Where is the Find
method on IEnumerable<T>
? (Rhetorical question.)
The Where
and FirstOrDefault
methods are applicable against multiple kinds of sequences, including List<T>
, T[]
, Collection<T>
, etc. Any sequence that implements IEnumerable<T>
can use these methods. Find
is available only for the List<T>
. Methods that are generally more applicable, are then more reusable and have a greater impact.
I guess my next question would be why did they add the find at all. That is a good tip. The only thing I can think of is that the FirstOrDefault could return a different default value other than null. Otherwise it just seems like a pointless addition
Find
on List<T>
predates the other methods. List<T>
was added with generics in .NET 2.0, and Find
was part of the API for that class. Where
and FirstOrDefault
were added as extension methods for IEnumerable<T>
with Linq, which is a later .NET version. I cannot say with certainty that if Linq existed with the 2.0 release that Find
would never have been added, but that is arguably the case for many other features that came in earlier .NET versions that were made obsolete or redundant by later versions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…