The C# return-yield Mechanism

I have become a believer. For a long time (since .NET 2.0) I have resisted using the C# yield keyword. My thought was that the return-yield mechanism hid the details of what was actually going on. However, over the past few days I have been working on some code which requires the use of return-yield because I am implementing an Interface. Anyway, I was forced to really dive in and understand yield, and like many things, once I fully understood the mechanism I really learned to appreciate it.
In my mind, the return-yield mechanism is really useful when you are programmatically generating very large amounts of data. The example I am working on has a monstrously huge external data source and I need to iterate through the source and compute derived data. I could never fit the entire result set into memory, but the return-yield mechanism effectively allows me to stream my results as needed. In some sense the return-yield mechanism is similar to the way you can stream a text file one line at a time. In either case you could write your own buffering code, but this is fairly time-consuming and error-prone. There are plenty of good blog postings about the mechanics of writing and calling return-yield code.
This entry was posted in Software Test Automation. Bookmark the permalink.