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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:07:51+00:00 2026-06-15T17:07:51+00:00

While I was making a logic to iterate two enumerables, with different types, through

  • 0

While I was making a logic to iterate two enumerables, with different types, through a comparison, I found this:

class Program
{
    public static IEnumerable<mm> YieldlyGet()
    {
        yield return new mm { Int = 0 };
        yield return new mm { Int = 1 };
        yield return new mm { Int = 2 };
        yield return new mm { Int = 3 };
        yield return new mm { Int = 4 };
        yield return new mm { Int = 5 };
    }

    public static IEnumerable<int> YieldlyGetInt()
    {
        yield return 0;
        yield return 1;
        yield return 2;
        yield return 3;
        yield return 4;
        yield return 5;
    }

    public static IEnumerable<int> Get() 
    {
        return new List<int> { 0, 1,2,3,4,5 };
    }

    static void Main(string[] args) 
    {
        var yieldr = YieldlyGet().GetEnumerator();
        var yieldv = YieldlyGetInt().GetEnumerator();

        var list = Get().GetEnumerator();

        int i = -1;
        Console.WriteLine("For the current index: {0}", ++i);
        Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
        Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
        Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);

        Console.WriteLine("For the current index: {0}", ++i);
        Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
        Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
        Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);

        Console.WriteLine("For the current index: {0}", ++i);
        Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
        Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
        Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);

        Console.WriteLine("For the current index: {0}", ++i);
        Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
        Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
        Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);

        Console.WriteLine("For the current index: {0}", ++i);
        Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
        Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
        Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);

        Console.WriteLine("For the current index: {0}", ++i);
        Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
        Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
        Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);

        Console.WriteLine("For the current index: {0}", ++i);
        Console.WriteLine("y-r: Should I move next? {0}, if yes, value: {1}", yieldr.MoveNext(), yieldr.Current != null ? yieldr.Current.Int : 0);
        Console.WriteLine("y-v: Should I move next? {0}, if yes, value: {1}", yieldv.MoveNext(), yieldv.Current);
        Console.WriteLine("l: Should I move next? {0}, if yes, value: {1}", list.MoveNext(), list.Current);


        Console.ReadLine();
}

The thing is that, when I am after the last position, the list presents me with the default, whilst the yield created Iterator, keeps showing the last value.

For the current index: 6
y-r: Should I move next? False, if yes, value: 5
y-v: Should I move next? False, if yes, value: 5
l: Should I move next? False, if yes, value: 0

Why?

  • 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-15T17:07:52+00:00Added an answer on June 15, 2026 at 5:07 pm

    Let’s look what happen in enumerator, which used by List<T> class (this is a List<int>.Enumerator). Actually msdn says that current value will be undefined, but we can analyze Framework 4.0 implementation. So, when enumerator is positioned after last element MoveNextRare will be called:

    public bool MoveNext()
    {
        List<T> ts = this.list;
        if (this.version != ts._version || this.index >= ts._size)
        {
            return this.MoveNextRare();
        }
        else
        {
            this.current = ts._items[this.index];
            List<T>.Enumerator<T> enumerator = this;
            enumerator.index = enumerator.index + 1;
            return true;
        }
    }
    

    Thus list is valid (unchanged) this method returns defaut(T) value (which is 0 for int)

    private bool MoveNextRare()
    {
        if (this.version != this.list._version)
            trow new InvalidOperationException();
    
        this.index = this.list._size + 1;
        this.current = default(T);
        return false;
    }
    

    What about generated enumerators? C# generates enumerator class, which will have states for each yield return statements. Moving to next state sets Current value for this enumerator:

    bool MoveNext()
    {
        bool flag;
        int state = this.state;
        if (state == 0)
        {
            this.state = -1;
            this.current = 0;
            this.state = 1;
            flag = true;
        }
        else if (state == 1)
        {
            this.state = -1;
            this.current = 1;
            this.state = 2;
            flag = true;
        }
        // ...
        else if (state == 5)
        {
            this.state = -1;
            this.current = 5;
            this.state = 6;
            flag = true;
        }
        else if (state == 6)
        {
            this.state = -1;
            flag = false;
            return flag;
        }
        else
        {
            flag = false;
            return flag;
        }
        return flag;
        flag = false;
        return flag;
    }
    

    What is interesting here is that this.current is not changed, after last assignment (when state was 5). That’s why all subsequent calls to Current will return value, which was set by last yield return call.

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

Sidebar

Related Questions

While making my way through the wonderful world of IndexedDB, I came across code
While making some final tests of a class-library that I'm writing for Windows Mobile
I noticed while making a program that a lot of my int type variables
I just ran into this issue while making a GET request to a node.js
I am making a simplified version of the ball physics app found at this
While making a little Pong game in C++ OpenGL, I decided it'd be fun
While making AutoHotkey-script I encountered the following problem. I need navigate listbox (one position
Iam getting OutOfMemoryException while making remote method call. RemoteEntity.SetLocalStore(DATASET); passed value is dataset. Note
I stumbled on a way to crash excel in Workbook_Open while making an .xla
I'm starting with Ruby, and while making some test samples, I've stumbled against an

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.