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 9111953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:37:18+00:00 2026-06-17T03:37:18+00:00

Suppose I have some strings: string[] strings = { zero, one, two, three, four,

  • 0

Suppose I have some strings:

string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

What is the difference between:

string startsWithO = strings.First(s => s[0] == 'o');

And:

string startsWithO = strings.Where(s => s[0] == 'o').First();

Since Where() is deferred it shouldn’t slow down the execution, right?

  • 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-17T03:37:19+00:00Added an answer on June 17, 2026 at 3:37 am

    The performance penalty of using .Where(filter).First() rather than .First(filter) will usually be very small.

    However, they’re not the same – Where generates a new iterator that First can simply take one element of, whereas First(filter) can microoptimize by using just one loop and a direct return whenever filter matches.

    So while both approaches have the same semantics and both execute the filter equally often (only as often as necessary), using First with a filter parameter doesn’t need to create an intermediate iterator object and probably avoids some very simple method calls on that iterator too.

    In other words, if you’re executing such code millions of times, you will see a slight performance difference – but nothing huge; I would never worry about it. Whenever that tiny performance difference actually matters you’re much better off just writing the (very simple) foreach-with-if statement that’s equivalent and avoiding the extra calls and object allocations inherent in LINQ – but remember that this is a microoptimization you’ll rarely need.

    Edit: Benchmark demonstrating the effect:

    This takes 0.78 seconds:

    for(int i=0;i<10*1000*1000;i++)
      Enumerable.Range(0,1000).First(n=> n > 2);
    GC.Collect();
    

    But this takes 1.41 seconds:

    for(int i=0;i<10*1000*1000;i++)
      Enumerable.Range(0,1000).Where(n=> n > 2).First();
    GC.Collect();
    

    Whereas plain loops are much faster (0.13 seconds):

    long bla = 0;
    for(int i=0;i<10*1000*1000;i++)
        for(int n=0;n<1000;n++)
            if(n > 2) { bla+=n; break; }
    GC.Collect();
    Console.WriteLine(bla);//avoid optimizer cheating.
    

    Note that this benchmark only shows such extreme differences because I have a trivial filter and a very short non-matching prefix.

    Based on some quick experimentation, the difference seems largely attributable to the details of which codepath gets taken. So, for array’s and List<>s the first variant is actually faster, likely to do special-casing in .Where for those types that First doesn’t have; for custom iterators, the second version is a tiny bit faster, as expected.

    Summary:

    .Where(...).First() is roughly as fast as .First(...) – don’t bother choosing one or the other as an optimization. In general .First(...) is very slightly faster but in some common cases it is slower. If you really need that microoptimization then use plain loops which are faster than either.

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

Sidebar

Related Questions

suppose I have this string: some striinnngggg <a href=something/some_number>linkk</a> soooo <a href=someotherthing/not_number>asdfsadf</a> I want
Suppose I have the following code: foreach(string str in someObj.GetMyStrings()) { // do some
Suppose I have this function: std::string Func1(std::string myString) { //do some string processing std::string
Suppose we have an Option[String], and if there is Some(string) in there, we want
Suppose I have some schema: <xsd:schema ...> <xsd:element name=foo> <xsd:complexType> <xsd:sequence> <xsd:element name=fooElement type=xs:string
Suppose I have the following list: var strings = new List<string>(); strings.Add(1); strings.Add(12.456); strings.Add(Foobar);
Suppose I have a collection of strings. List<string> lines = new List<string>(File.ReadAllLines(Test.txt)); And a
Suppose I have two projects. One is an application and the other is a
Suppose I have a char pointer which points to some string in memory. and
Suppose I have some code like this: public string SomeMethod(int Parameter) { string TheString

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.