Can anyone tell me when to use Find() and When to use First() ? Is one better than the other if you just want a simple query to find a specific item in the list?
FirstOrDefault usually perform faster as compared SingleOrDefault, since these iterate the collection until they find the first match. While SingleOrDefault iterate the whole collection to find one single match.
Find() runs almost as twice as faster, hoping . Net team will not mark it Obsolete in the future. Try timing Find() before FirstOrDefault . What are the results then?
The SingleOrDefault() method does the same thing as Single() method. The only difference is that it returns default value of the data type of a collection if a collection is empty, includes more than one element or finds no element or more than one element for the specified condition.
Syntax of LINQ FIRST () method The syntax of the first method to get the first element from the list is: int result = objList. First();
If it's possible for the default value of the type of your list (that is, null for reference types, zero for numeric types, and the result of calling the default constructor for other value types) to be a correct "found it" result, and if it's important that you distinguish this from it not being found, then you need to use First()
as it will throw an exception on not finding anything, while Find()
will return the default value.
So. Do you want an exception in this case or no?
That distinction aside, Find()
is pretty much the same as FirstOrDefault()
, which also returns the default when it finds nothing.
Use Find()
in .NET2.0, since you don't have FirstOrDefault()
.
Use Find()
if it's about the only thing you'd be adding a reference to System.Core
for, though this is hardly a big issue (it's not like System.Core
isn't commonly used and available with all the framework versions).
Use FirstOrDefault()
if you want to call it on something other than a List<T>
.
Use FirstOrDefault()
if you might want to call it on something other than a L
ist<T>
in the future.
Use Find()
if you really care about the trivial performance difference, but if you do you'd be better writing your own method that spun through the list and didn't use a delegate anyway.
Use FirstOrDefault()
if your predicate is item => true
, because you can call the form that takes no predicate. That said, you could use list[0]
anyway.
Use FirstOrDefault()
if you're mixing it in with other Linq, as it'll read more consistent that way.
None of the above are exactly earth-shattering differences though. Really, the difference between the two is pretty minor, and I doubt Find()
would have been written had FirstOrDefault()
been added in the same framework version.
First()
returns first element from the sequence or first from a sequence that satisfy the condition. The method First()
throws an exception when sequence is empty. The Find()
method searches an element that matches the condition. If search element is not found then it returns default value for type T
.
Some cool SO threads:
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