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?