Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Collections.Generic.Find() vs Linq.First()

Tags:

c#

.net

linq

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?

like image 627
Kjetil Salomonsen Avatar asked Jan 12 '12 11:01

Kjetil Salomonsen


People also ask

Which is faster SingleOrDefault or FirstOrDefault?

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.

Is find faster than FirstOrDefault?

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?

What is difference between single and SingleOrDefault in Linq?

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.

Which Linq method retrieves the first object of a collection?

Syntax of LINQ FIRST () method The syntax of the first method to get the first element from the list is: int result = objList. First();


2 Answers

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.

like image 180
Jon Hanna Avatar answered Sep 26 '22 01:09

Jon Hanna


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:

  1. LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria
  2. Regarding FirstOrDefault or SingleOrDefault
like image 40
KV Prajapati Avatar answered Sep 24 '22 01:09

KV Prajapati