5

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433

4 Answers4

10

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.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
3

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
Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

First() returns the first element from the sequence, throwing an exception if the source sequence is empty or no match is found. Find() method searches for an element that matches the condition returning the default value for the type if no match is found.

Ɖiamond ǤeezeƦ
  • 3,223
  • 3
  • 28
  • 40
0

They basically do the same thing, with the exception of First() throwing an exception if the match does not exist. FirstOrDefault is pretty much the match for Find().

For anyone interested, First() and Find() work in a slightly different manner though.

This is the (approximate) find loop in Find() extracted from List<T>. It uses array indexing.

    int num = 0;
    num++;
    while (num < this._size)
        if (match(this._items[num]))
            return this._items[num];
    T t = default(T);

    return t;

First() uses enumeration and thus works on any enumerable collection

    foreach (TSource tSource in source)
        if (predicate(tSource))
            return tSource;

    throw Error.NoMatch();

This should allow for Find() to have a slight speed advantage due to no Enumerator overhead.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Surely you mean "this should allow `Find()` to have a slight speed advantage"? – Jon Hanna Jan 12 '12 at 12:37
  • 1
    I do it all the time. Or I either add or omit a "not" and end up saying the opposite to what I mean :) – Jon Hanna Jan 12 '12 at 12:51
  • 1
    @JoachimIsaksson nice catch! I ran a few tests and the Find is really faster and it's not somewhat slight, there's a solid difference. Find runs almost as twice as faster. Pls see [here](http://stackoverflow.com/questions/14032709/performance-of-find-vs-firstordefault) – Arman Dec 25 '12 at 17:41