You are previewing C# in Depth, Second Edition.

C# in Depth, Second Edition

Cover of C# in Depth, Second Edition by Jon Skeet Published by Manning Publications
O'Reilly logo

Chapter 6. Implementing iterators the easy way

This chapter covers

  • Implementing iterators in C# 1
  • Iterator blocks in C# 2
  • Sample iterator usage
  • Iterators as coroutines

The iterator pattern is an example of a behavioral pattern—a design pattern that simplifies communication between objects. It’s one of the simplest patterns to understand, and incredibly easy to use. In essence, it allows you to access all the elements in a sequence of items without caring about what kind of sequence it is—an array, a list, a linked list, or none of the above. This can be effective for building a data pipeline, where an item of data enters the pipeline and goes through a number of different transformations or filters before coming out at the other end. Indeed, ...

The best content for your career. Discover unlimited learning on demand for around $1/day.