Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9039965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:51:59+00:00 2026-06-16T09:51:59+00:00

Got an interesting outcome searching for Diana within a large sequence of a simple

  • 0

Got an interesting outcome searching for Diana within a large sequence of a simple reference type having a single string property.

using System;
using System.Collections.Generic;
using System.Linq;

public class Customer{
    public string Name {get;set;}
}

Stopwatch watch = new Stopwatch();        
    const string diana = "Diana";

    while (Console.ReadKey().Key != ConsoleKey.Escape)
    {
        //Armour with 1000k++ customers. Wow, should be a product with a great success! :)
        var customers = (from i in Enumerable.Range(0, 1000000)
                         select new Customer
                         {
                            Name = Guid.NewGuid().ToString()
                         }).ToList();

        customers.Insert(999000, new Customer { Name = diana }); // Putting Diana at the end :)
                
        //1. System.Linq.Enumerable.DefaultOrFirst()
        watch.Restart();
        customers.FirstOrDefault(c => c.Name == diana);
        watch.Stop();
        Console.WriteLine("Diana was found in {0} ms with System.Linq.Enumerable.FirstOrDefault().", watch.ElapsedMilliseconds);

        //2. System.Collections.Generic.List<T>.Find()
        watch.Restart();
        customers.Find(c => c.Name == diana);
        watch.Stop();
        Console.WriteLine("Diana was found in {0} ms with System.Collections.Generic.List<T>.Find().", watch.ElapsedMilliseconds);
    }

enter image description here

Is this because of no Enumerator overhead in List.Find() or this plus maybe something else?

Find() runs almost as twice as faster, hoping .Net team will not mark it Obsolete in the future.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:52:00+00:00Added an answer on June 16, 2026 at 9:52 am

    I was able to mimic your results so I decompiled your program and there is a difference between Find and FirstOrDefault.

    First off here is the decompiled program. I made your data object an anonmyous data item just for compilation

        List<\u003C\u003Ef__AnonymousType0<string>> source = Enumerable.ToList(Enumerable.Select(Enumerable.Range(0, 1000000), i =>
        {
          var local_0 = new
          {
            Name = Guid.NewGuid().ToString()
          };
          return local_0;
        }));
        source.Insert(999000, new
        {
          Name = diana
        });
        stopwatch.Restart();
        Enumerable.FirstOrDefault(source, c => c.Name == diana);
        stopwatch.Stop();
        Console.WriteLine("Diana was found in {0} ms with System.Linq.Enumerable.FirstOrDefault().", (object) stopwatch.ElapsedMilliseconds);
        stopwatch.Restart();
        source.Find(c => c.Name == diana);
        stopwatch.Stop();
        Console.WriteLine("Diana was found in {0} ms with System.Collections.Generic.List<T>.Find().", (object) stopwatch.ElapsedMilliseconds);
    

    The key thing to notice here is that FirstOrDefault is called on Enumerable whereas Find is called as a method on the source list.

    So, what is find doing? This is the decompiled Find method

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

    So it’s iterating over an array of items which makes sense, since a list is a wrapper on an array.

    However, FirstOrDefault, on the Enumerable class, uses foreach to iterate the items. This uses an iterator to the list and move next. I think what you are seeing is the overhead of the iterator

    [__DynamicallyInvokable]
    public static TSource FirstOrDefault<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 source1 in source)
      {
        if (predicate(source1))
          return source1;
      }
      return default (TSource);
    }
    

    Foreach is just syntatic sugar on using the enumerable pattern. Look at this image

    enter image description here.

    I clicked on foreach to see what it’s doing and you can see dotpeek wants to take me to the enumerator/current/next implementations which makes sense.

    Other than that they are basically the same (testing the passed in predicate to see if an item is what you want)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an interesting issue with type comparison. I'm attempting to compare an implied
I got an interesting request from the higher ups. They want a simple app
Hi all I've got an interesting task to update a single column in a
I tested the following code in firefox scratchpad and got interesting result? var date=new
I got an interesting problem: file1.csv has a few hundred rows like: Code,DTime 1,2010-12-26
I've got an interesting SQL puzzle I haven't been able to solve, hopefully one
I've got an interesting box-model problem here. I have a header full of links,
Unusable links with onmouseover() got an interesting question, when I tried to answer it.
I got into an interesting discussion in a forum where we discussed the naming
I've stumbled upon an interesting problem while playing with my website. So I've got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.