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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:30:20+00:00 2026-05-19T11:30:20+00:00

I was going through Jon Skeet’s Reimplemnting Linq to Objects series . In the

  • 0

I was going through Jon Skeet’s Reimplemnting Linq to Objects series. In the implementation of where article, I found the following snippets, but I don’t get what is the advantage that we are gettting by splitting the original method into two.

Original Method:

// Naive validation - broken! 
public static IEnumerable<TSource> Where<TSource>( 
    this IEnumerable<TSource> source, 
    Func<TSource, bool> predicate) 
{ 
    if (source == null) 
    { 
        throw new ArgumentNullException("source"); 
    } 
    if (predicate == null) 
    { 
        throw new ArgumentNullException("predicate"); 
    } 
    foreach (TSource item in source) 
    { 
        if (predicate(item)) 
        { 
            yield return item; 
        } 
    } 
}

Refactored Method:

public static IEnumerable<TSource> Where<TSource>( 
    this IEnumerable<TSource> source, 
    Func<TSource, bool> predicate) 
{ 
    if (source == null) 
    { 
        throw new ArgumentNullException("source"); 
    } 
    if (predicate == null) 
    { 
        throw new ArgumentNullException("predicate"); 
    } 
    return WhereImpl(source, predicate); 
} 

private static IEnumerable<TSource> WhereImpl<TSource>( 
    this IEnumerable<TSource> source, 
    Func<TSource, bool> predicate) 
{ 
    foreach (TSource item in source) 
    { 
        if (predicate(item)) 
        { 
            yield return item; 
        } 
    } 
} 

Jon says – Its for eager validation and then defferring for the rest of the part. But, I don’t get it.

Could some one please explain it in a little more detail, whats the difference between these 2 functions and why will the validations be performed in one and not in the other eagerly?

Conclusion/Solution:

I got confused due to my lack of
understanding on which functions are
determined to be iterator-generators.
I assumed that, it is based on
signature of a method like
IEnumerable<T>. But, based on the
answers, now I get it, a method is an
iterator-generator if it uses yield
statements.

  • 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-05-19T11:30:20+00:00Added an answer on May 19, 2026 at 11:30 am

    The broken code is a single method, really an iterator-generator. That means it initially just returns a state machine without doing anything. Only when the calling code calls MoveNext (likely as part of a for-each loop) does it execute everything from the beginning up to the first yield-return.

    With the correct code, Where is not an iterator-generator. That means it executes everything immediately, like normal. Only WhereImpl is. So the validation is executed right away, but the WhereImpl code up to and including the first yield return is deferred.

    So if you have something like:

    IEnumerable<int> evens = list.Where(null); // Correct code gives error here.
    foreach(int i in evens) // Broken code gives it here.
    

    the broken version won’t give you an error until you start iterating.

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

Sidebar

Related Questions

Going through Javascript documentation, I found the following two functions on a Javascript object
I'm reading through Jon Skeet's book reviews and he is going over the numerous
Just going through the sample Scala code on Scala website, but encountered an annoying
While going through Effective C++ (by Scott Meyers), I came across the following code
While going through a WP7.5 background transfer service policy, one can read the following:
While going through a Zend tutorial , I came across the following statement: Note
Going through the documentation of the SDF, i found many classes like the BitmapEx
While going through the checkBox I found there is written CheckBox checkbox = (CheckBox)sender
Going through this article class ThreadTest { static void Main() { Thread t =
While Going through the java tutorial on sun site, I see following piece of

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.