Cursor-style iteration pattern
Note: this entry has moved.
The usual enumeration pattern in .NET is either of these:
When working with cursor-style APIs like the XmlReader or XPathNavigator, the pattern usual becomes something like this (note this is the same code for both APIs):
This is not as readable or compact as the usual approach.
One solution to get back the for style of
iteration, which I use all the time, is the following:
With this approach, you can the usual iteration pattern again, and have more compact and readable code. It can be used with all XPathNavigator methods that use MoveToFirst/MoveToNext: