Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
C#
hi
i was watching a video that was a simulation of where and select expression of linq to objects with extension methods and here is the code

using System;
using System.Collections.Generic;
public static class Program
{
    static IEnumerable<T> Where<T>(this IEnumerable<T> items, Func<T, bool> gauntlet)
    {
        Console.WriteLine("where");
        foreach (T item in items)
        {
            if (gauntlet(item))
                yield return item;
        }
    }

    static IEnumerable<R> Select<T,R>(this IEnumerable<T> items, Func<T, R> transform)
    {
        Console.WriteLine("select");
        foreach (T item in items)
                yield return transform(item);
 
    }
    static void Main()
    {
        int[] stuff = { 4, 8, 1, 9 };
        IEnumerable<int> result =
            stuff
            .Where(i => i < 5)
            .Select(i => i);

        IEnumerator<int> rator = result.GetEnumerator();

        while (rator.MoveNext())
            Console.WriteLine(rator.Current);
        Console.ReadLine();
    }  
}

when i run this code i get the  following output
select
where
4
1

but i expect this output to be 
where
select
4
1

so i bet  the yield keyword is cooking something somewhere so would plz explain to me why is it happening from the end to the beginning? and not vice versa and the role of yield in there?


What I have tried:

i tried to understand this code but i could not
Posted
Updated 11-Jun-16 2:31am
Comments
Philippe Mori 11-Jun-16 8:48am    
Please fix documentation and sample formatting. Code block should contains code and sample output could use "text" as the code language. You should make some effort to give a better presentation to your questions.
Philippe Mori 11-Jun-16 8:59am    
Do not write words like "plz" on forum. As far as I know there is a rule for the forum that say that we should not use abbreviations.

It may be useful for you to think of Linq operations that return IEnumerable-something-or-others as returning a kind of "abstract map" of how to assemble the eventual "actual" result ... that's the "deferred" part. What triggers evaluation and returns the "actual" result can be conversion to Array, or List, or Dictionary via the ToArray, ToList, ToDictionary operators, or, it can be enumerating the IEnumerable through iterating over its contents with a for-loop, a for-each loop, etc.

There is a kind of semantic overlap here that some find confusing because we are used to I-something-or-others being Interfaces, and IEnumerable has an additional level of behavior that Interfaces do not share, as well as the common factor that they are both, in a way, "abstractions."
 
Share this answer
 
 
Share this answer
 
Comments
Philippe Mori 11-Jun-16 8:57am    
In what does reading yield documentation would help understand why "where" is displayed before or after "select"?
Richard MacCutchan 11-Jun-16 9:08am    
Final paragraph in OP's question.
Philippe Mori 11-Jun-16 9:33am    
Well, "Technical Implementation" paragraph give an hint...
Richard MacCutchan 11-Jun-16 9:49am    
Please feel free to post your better solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900