Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Difference between First() and Find()

Tags:

c#

list

linq

c#-4.0

So I know that Find() is only a List<T> method, whereas First() is an extension for any IEnumerable<T>. I also know that First() will return the first element if no parameter is passed, whereas Find() will throw an exception. Lastly, I know that First() will throw an exception if the element is not found, whereas Find() will return the type's default value.

I hope that clears up confusion about what I'm actually asking. This is a computer science question and deals with these methods at the computational level. I've come to understand that IEnumerable<T> extensions do not always operate as one would expect under the hood. So here's the Q, and I mean from a "close to the metal" standpoint: What is the difference between Find() and First()?

Here's some code to provide basic assumptions to operate under for this question.

var l = new List<int> { 1, 2, 3, 4, 5 }; var x = l.First(i => i == 3); var y = l.Find(i => i == 3); 

Is there any actual computational difference between how First() and Find() discover their values in the code above?

Note: Let us ignore things like AsParallel() and AsQueryable() for now.

like image 699
Squirrelsama Avatar asked Dec 06 '10 17:12

Squirrelsama


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


1 Answers

Here's the code for List<T>.Find (from Reflector):

public T Find(Predicate<T> match) {     if (match == null)     {         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);     }     for (int i = 0; i < this._size; i++)     {         if (match(this._items[i]))         {             return this._items[i];         }     }     return default(T); } 

And here's Enumerable.First:

public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) {     if (source == null)     {         throw Error.ArgumentNull("source");     }     if (predicate == null)     {         throw Error.ArgumentNull("predicate");     }     foreach (TSource local in source)     {         if (predicate(local))         {             return local;         }     }     throw Error.NoMatch(); } 

So both methods work roughly the same way: they iterate all items until they find one that matches the predicate. The only noticeable difference is that Find uses a for loop because it already knows the number of elements, and First uses a foreach loop because it doesn't know it.

like image 171
Thomas Levesque Avatar answered Oct 14 '22 17:10

Thomas Levesque