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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:33:55+00:00 2026-05-13T18:33:55+00:00

If I use yield instead of manually creating an IEnumerator, is it possible to

  • 0

If I use yield instead of manually creating an IEnumerator, is it possible to implement IEnumerator.Reset?

  • 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-13T18:33:56+00:00Added an answer on May 13, 2026 at 6:33 pm

    There is no built-in support, but you can define your own implementation of IEnumerator that delegates all method calls to the enumerator generated by C# and only lets you define your own behavior for the Reset method.

    The simplest version of the class would look like this:

    class ResetableEnumerator<T> : IEnumerator<T>
    {
      public IEnumerator<T> Enumerator { get; set; }
      public Func<IEnumerator<T>> ResetFunc { get; set; }
    
      public T Current { get { return Enumerator.Current; } }
      public void  Dispose() { Enumerator.Dispose(); }
      object IEnumerator.Current { get { return Current; } }
      public bool  MoveNext() { return Enumerator.MoveNext(); }
      public void  Reset() { Enumerator = ResetFunc(); }
    }
    

    In this case, the ResetFunc that you specify returns a new IEnumerator<T>, so your provided implementation of ResetFunc can do some cleanup or whatever you need to do when resetting and then return a new enumerator.

    IEnumerator<int> Foo() { /* using yield return */ }
    IEnumerator<int> PublicFoo() {
      return new ResetableEnumerator<int> { 
        Enumerator = Foo(),
        ResetFunc = () => { 
          Cleanup();
          return Foo(); } };
    }
    

    You’ll need to store all the originally local variables of the Foo method as fields of the class, so that you can access them in Cleanup (Note that the rest of the Foo body will never be executed after calling Reset), but that’s still easier than writing a handwritten iterator!

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

Sidebar

Related Questions

Possible Duplicate: Proper Use of yield return What is the use of the yield
how can I remove this yield's? I wanna use a map instead of: val
Can I use yield return when the return type is an IGrouping<TKey, TElement> or
I'm having a little trouble with a method in which I use yield return
we use (1..10).each do |i| p i end so that a value is yield
In Ruby I can use result << (yield element) and everything works, but if
I just found out about yield return , I seems really nice. I use
I have a simple yield use case and for some unknown reason the default
When is it appropriate to use a ThrowHelper method instead of throwing directly? void
still trying to find where i would use the yield keyword in a real

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.