Code Focused

Understanding Iterators: Concepts, Benefits and Functionality

Iterators are available for Visual Basic in Visual Studio 2010 after installing the latest Visual Studio Async CTP, and natively in Visual Basic 11.

Iterators in both C# and Visual Basic provide a simple mechanism to asynchronously consume the elements of a collection as they become available, rather than wait for the collection to be fully formed and then processed. Iterators provide a very easy-to-code method to improve application performance via asynchronous processing in the specific case of creating and consuming item collections.

Iterators clearly provide benefits when the collection takes too long to populate, and risks making the application seem unresponsive to the user. How long is too long? It depends on the application and developer preference, but Microsoft used 50 milliseconds (0.05 seconds) as the maximum expected response time when designing the WinRT methods for Windows 8. Any routine that could reasonably take longer than that was coded as an asynchronous method. Correspondingly, I recommend the use of iterators if it takes longer than 50 milliseconds to fully populate the collection in your application. If populating the collection takes significantly less than 50 milliseconds, then it may not be worth the slightly increased programming and runtime overhead to use iterators.

Iterators may also provide benefit when the individual collection item requires significant amounts of memory. By consuming each item as it becomes available, the application can avoid allocating memory for the full collection and any associated performance penalties.

Prior to iterators, an application would have had to invoke additional threads in order to perform asynchronous operations for a responsive user experience. That came at a heavy price in terms of additional programming complexity, enough that I completely avoided writing multi-threaded applications. Iterators don't invoke additional threads, and thus don't introduce the unwanted complexity.

If no additional threads are created, how do iterators provide a more responsive result? A somewhat simplified explanation is that wherever an iterator has an item ready for the calling method to process, the developer coded a Yield {collection item} statement. The compiler remembers the location of the Yield statement and the state of the iterator method, then provides the collection item specified in the Yield statement to the calling method.

When the calling statement requests the next element in the collection, the compiler resumes execution at the remembered location within the iterator and begins the process of forming the next collection item value. Note that this iterator processing is being performed by the compiler and is not a feature of the .NET Framework or the Common Language Runtime (CLR).

From the above description you can see that the time to complete processing of the full collection will require essentially the same amount of time as it would have without iterators (plus the slight overhead of switching execution flow), but that the individual elements are presented much faster. For example, an iterator function may return one item each second for a 10 item collection, rather than return all 10 items only after 10 seconds have elapsed as a standard function would have done.

Iterators have been available in C# since Visual Studio 2005 but are new to Visual Basic developers. The explanation in Part 1 of this article series applies equally for both C# and Visual Basic developers. In Part 2, I'll specifically explore Visual Basic iterator syntax, demonstrate Visual Basic iterator methods and properties, and compare Visual Basic iterators to C# iterators.

The Visual Studio Async CTP (Version 3) can be found at

About the Author

Joe Kunk is a Microsoft MVP in Visual Basic, three-time president of the Greater Lansing User Group for .NET, and developer for Dart Container Corporation of Mason, Michigan. He's been developing software for over 30 years and has worked in the education, government, financial and manufacturing industries. Kunk's co-authored the book "Professional DevExpress ASP.NET Controls" (Wrox Programmer to Programmer, 2009). He can be reached via email at [email protected].

comments powered by Disqus


Subscribe on YouTube