Iterator Usage

OK, a few days ago I asked about how you used anonymous methods.  Thanks for the very few who responded.  I'm hoping that more people have used them than chose to respond.  Now it's time to ask about my other pet feature: Iterators.

How an possibly more importantly why do you use iterators?  I'm sure that a good programmer could always sit down and write a better implementation of IEnumerable/IEnumerator for their given data.  I wonder if that time is really well spent.  Ideally iterators would allow you write comparable performing code in significantly less time.  Especially if you count the time spent to debug and fix the inevitable bugs and correctness issues.

There are valid reasons to not use iterators, just like sometimes you need to use a for loop instead of a foreach loop.  I've bumped into these a few times, but usually I've changed other things so I can use an iterator, rather than trying to write the IEnumerable/IEnumerator code myself. I want to know how often these situations come up in real world code, and how you resolve them.  Do you change your code so you can use an iterator, or do you hand-write the enumerator?

I use iterators because they save me time and I have fewer bugs.  Why do you use them, or why don't you use them?


Comments (5)

  1. My very first reason would be that an iterator is very clean code. IEnumerators require an extra class and code split up across several methods. That is no where near the clarity that a iterator provides, with a single inline method.

    Secondly, they certainly are easier to write.

    Also, I am pretty inclined not to use an iterator so much as a IEnumerable replacement, but as a generator method. Streams of values, either loaded from disk or generated by the iterator is a useful practice and is a valid reason for iterators on their own(IEnumerator could do this, but it would be considerably more work).

    I can’t think of a specific reason to need to write an enumerator by hand now. I’m sure they crop up but the vast majority of the time I’ve yet to need one.

    So, I guess ease of use and clarity are my reasons.

  2. Maybe it’s just me and my being used to C# 1.0, but iterators are the feature that fits in the least into the language in my opinion, since it blocks the flow of the program.

    In my limited experience with them, iterators are a bit hard to read, so I’ll try and avoid them as much as possible in the near future.

    As for your specific question, I think that a nice scenario for this would be the ability to use filters for the collection not on the items that are taken by iteration but on the iterated object itself.

    Another example would be if I have an object O that has states A and B and the iteration on the object would be according to the object’s state (filter, sorting, etc).

  3. I was going to respond to the anonymous methods question, but I don’t check this blog every day and I thought I was too late. Anyway, I use them to avoid having to write a one-or-two line event handler, but that’s about it 🙂

    For iterators, I don’t anticipate using them in the foreseeable future. They are a handy alternative to the IEnumerable/IEnumerator problem, but I’ve never had the need to write one of those in the first place.

    With the addition of generics, the base types are now a lot more useful and should satisfy my enumeration needs.

  4. At first, I thought iterators were just a quite nice idea. After seeing how C-omega uses them I suddenly realize they are much more versatile than I thought at first. The issue that caused me not to realize their true power is just a naming issue: the name ‘iterator’ doesn’t immediately associate with ‘stream of objects’, as C-omega uses them.

    That being said, I actually haven’t used them at all, for the same reason I haven’t used anonymous methods: most of my work involves writing a large amount of ‘small’ programs (mostly console apps falling in the categories ‘data conversion’ and ‘signal analysis’), for use by myself and my co-workers for our everyday work; though these tools are not visible to our clients directly, the work we do for our clients does depend on them, and I do not yet want to commit to developing such tools on a framework that is in beta stage, nor do I want to burden my coworkers with having to install a beta version of .NET framework 2.0.

  5. Sam Wu says:

    I have a couple of places in my current project that would be perfect for iterators. I have a couple of methods that take IEnumerable arguments. Before I heard of Iterators in C# 2.0, I wrote some IEnumerable’s by hand. One wasn’t too bad when it was a simple one-level data structure. Then I tried to write one over a three-level algorithm. I got so fed up with saving, bookmarking where the enumerator’s were at, I ended up writing it normally using a triple nested foreach loop and using extra space to save the results.

    When I read about iterators, it just hit me! Now I can just iterate over my data structure as I would normally, still keep my algorithm independent of the data structure, and not worry about extra memory usage. Too bad you guys don’t have the final release out yet. I would looove to use generics, iterators. Can’t use beta for a production project….

Skip to main content