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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:07:20+00:00 2026-05-27T01:07:20+00:00

I know a lot has been written about Linq and it’s inner workings. Inspired

  • 0

I know a lot has been written about Linq and it’s inner workings. Inspired by Jon Skeets EduLinq, I wanted to to demystify what’s going on behind the Linq Operators. So what I tried to do is to implement Linqs Select() method which sounds pretty boring at first sight. But what I’m actually trying to do is to implement it without the use of the yield keyword.

So here is what I got so far:

class Program
{
    static void Main(string[] args)
    {
        var list = new int[] {1, 2, 3};

        var otherList = list.MySelect(x => x.ToString()).MySelect(x => x + "test");

        foreach (var item in otherList)
        {
            Console.WriteLine(item);
        }

        Console.ReadLine();
    }
}

public static class EnumerableEx
{
    public static IEnumerable<R> MySelect<T, R>(this IEnumerable<T> sequence, Func<T, R> apply)
    {
        return new EnumerableWrapper<R, T>(sequence, apply);
    }
}

public class EnumerableWrapper<T, O> : IEnumerable<T>
{
    private readonly IEnumerable<O> _sequence;
    private readonly Func<O, T> _apply;

    public EnumerableWrapper(IEnumerable<O> sequence, Func<O, T> apply)
    {
        _sequence = sequence;
        _apply = apply;
    }

    public IEnumerator<T> GetEnumerator()
    {
        return new EnumeratorWrapper<T, O>(_sequence, _apply);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public class EnumeratorWrapper<T, O> : IEnumerator<T>
{
    private readonly IEnumerator<O> _enumerator;
    private readonly Func<O, T> _apply;

    public EnumeratorWrapper(IEnumerable<O> sequence, Func<O, T> apply)
    {
        _enumerator = sequence.GetEnumerator();
        _apply = apply;
    }

    public void Dispose()
    {
    }

    public bool MoveNext()
    {
        var hasItems = _enumerator.MoveNext();
        if (hasItems)
            Current = _apply(_enumerator.Current);
        return hasItems;
    }

    public void Reset()
    {
        _enumerator.Reset();
    }

    public T Current { get; private set; }

    object IEnumerator.Current
    {
        get { return Current; }
    }
}

It seems to work. However, I have trouble to follow it’s control flow. As you can see, I chain to projections. This causes to let strange things (strange for me!) happen in the MoveNext() method. If you set breakpoints in every line of the MoveNext() method you will see that the control flow actually jumps between the different instances and never works through the method in one batch. It’s jumping as if it were using different threads or if we were using yield. But in the end, it’s just a normal method so I wonder what’s going on there?

  • 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-27T01:07:20+00:00Added an answer on May 27, 2026 at 1:07 am

    Each time you call MoveNext() on the result it will:

    • Call MoveNext() on the final iterator (which I’ll call Y), which will…
    • Call MoveNext() on the previous iterator (which I’ll call X), which will…
    • Call MoveNext() on the original iterator (of the array), which will return a number.
    • MoveNext() in X will then call the projection x => x.ToString() so that it has an appropriate Current member
    • MoveNext() in Y will then call the projection x => x + "test" on the result of X.Current, and store the result in Y.Current

    So there’s nothing particularly magical going on here – you’ve just got stacked calls, just as normal. The debugging experience will show you that call from one EnumeratorWrapper.MoveNext to another; the only odd jumping around will be when you step through the projections, which are declared in the Main method.

    If that doesn’t explain what was confusing you, please give more details about exactly where you don’t understand the flow, and I’ll see how I can help. (It’s great to see you wanting to learn more about how all these things work though. Good to see a kindred spirit!)

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

Sidebar

Related Questions

I know this has been discussed a lot of times but is there any
I know this question has been rised quite a lot of times, but then
Yes, I know this question has been asked a lot of times, but I
I know this topic has been beat to death but a lot of the
I know this has been covered a lot, but I just can't seem to
I know this has been asked a lot, but the only answers I could
I know this has been discussed a lot of times and to summarize: this
I know this question has been asked a lot, but not yet to my
I know this question has been asked before and I spent a lot of
Okay i know this has been asked a lot but it seems that none

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.