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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:48:32+00:00 2026-05-24T00:48:32+00:00

Let’s say I have collection with 100 elements. Regular enumerator would iterate over those

  • 0

Let’s say I have collection with 100 elements. Regular enumerator would iterate over those 100 elements.

I would like to create enumerator (which is based on the regular enumerator, i.e. it is not per each collection, but rather one, general approach) which scope is from "hehe" to "there" — and I could have for example iterate over 20 elements in the middle only.

void foo(IEnumerable<int> coll)
{
   var regular_iter = coll.GetEnumerator();
   regular_iter.MoveNext();
   regular_iter.MoveNext();
   // ... 8 more
   var scoped_iter = new ScopeEnumerator(regular_iterator,20);

So in such case when I call "scoped_iter.Reset()" it is reseted to its 0 element (10th for entire collection).

And also it "sees" only elements from 10-30.

The question is — how to implement such enumerator?

Edit

1.

I need iterator from "here", not from "there", because getting to "there" could be very time consuming. However this is minor thing really, the most problematic is Reset method.

2.

Jon asked about the background. What I really try to achieve is slicing the collection (i.e. you have the — let’s say — collection of 10 strings, but you would like to interpret it as collection of 5 elements, each element being collection of 2 strings). Naive algorithm is pretty simple, but also is very inefficient. With collection ~16MB (list of strings) I though about another approach — simply reinterpreting the data, without copying it. So I would create one iterator which picks every SIZE_OF_SLICE element from entire collection, and also I would create this scoped iterator which would start from the first iterator and go for SIZE_OF_SLICE elements.

This way data will be re-used in place, the only difference would be how you iterate over it. It is sufficient for slicing and it should be fast.

3

I implemented efficient slicing for IList (once you assume you have indexer, it is piece of cake) but it is disturbing me, you cannot (?) provide general efficient algorithm for both list (LinkedList) and arrays (List). So if you are reading this, and have an idea how to do it, do not hestitate to answer, even after 10 years (assuming C# will be still with us).

  • 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-24T00:48:33+00:00Added an answer on May 24, 2026 at 12:48 am

    To do this with the minimum of effort, you’d basically populate a collection which does support Reset (e.g. List<T>) with the iterator, and then return that.

    It’s slightly trickier to do that lazily – i.e. the first time you iterate, populate a collection. After the first reset, go into “replay” mode. I’m sure it’s feasible – it would just be slightly tricky.

    It would be even trickier if you had to support being reset the first time after only (say) 15 elements, then when you hit the 16th element the second time round, going back to the original iterator. Yikes.

    If you can scope out exactly what requirements you have, it could be a fun thing to implement though…

    EDIT: Just to pull some of the comments into this answer: you can’t do this in general without copying the data, because there’s no guarantee that the iterator will support being reset at all. Imagine if the iterator is providing data from some random number generator, or it’s a live broadcast which isn’t being recorded – obviously to replay the data, something would have to copy it.

    If you have a particular source implementation in mind, that may be different – but you can’t do it through just the IEnumerator<T> interface.

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

Sidebar

Related Questions

Let's say I have some content classes like Page, TabGroup, Tab, etc. Certain of
Let's say I need a 3-digit number, so it would be something like: >>>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say there is a graph and some set of functions like: create-node ::
Let's say I have table with column 'URL' whrere I store urls like this
Let's say that I have a set of relations that looks like this: relations
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say that I have classes like this: public class Parent { public int
Let's say I have an image called hello.png with dimensions 200x100 . I create
Let's say I dynamically create a timer like this: System.Timers.Timer expirationTimer = new Timer(expiration

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.