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 bit.ly/sVblDq.

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

Featured

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube