Prev | Current Page 315 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

The reason they??™ve lasted so long as an idea, however, is that they can
make code simpler to understand and more immediate. The balancing act between
complexity and simplicity is always a difficult one, and it??™s worth not being too ambitious
to start with. As anonymous methods and captured variables become more common,
we should all expect to get better at working with them and understanding what
they??™ll do. They??™re certainly not going away, and indeed LINQ encourages their use
even further.
Anonymous methods aren??™t the only change in C# 2 that involves the compiler creating
extra types behind the scenes, doing devious things with variables that appear to
be local. We??™ll see a lot more of this in our next chapter, where the compiler effectively
builds a whole state machine for us in order to make it easier for the developer
to implement iterators.
161
Implementing
iterators the easy way
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 very 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.


Pages:
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327