Current .Net Version: 2.0.50727
Home
Articles
C# & .Net Framework Reference
Practice C# Online
XML Web Services
RSS Feeds
Code Snippets
T-SQL Scripts
Videos
Valuable Links
Contact Us
Mike's C#, VS.NET and SQL Blog




ROR XML Info.
What's New in C# 2.0 - Iterators
Author: Michael G.     Last Updated:9/29/2005 8:03:35 PM
Category(ies): C# 2.0, .Net Framework 2.0
Description: Learn all about the new C# 2.0 Framework feature: Iterators.
  Add & View Comments About this Article

Tested with Microsoft Visual C# 2005 Beta 1, .Net 2.0.40607.42

Iterators allow you to easily create enumerable classes: classes that allow you to iterate through their collection of values via a foreach-in loop. Classes that implement the IEnumerable() interface allow you to use foreach-in against them. The IEnumerable interface demands that you implement a GetEmumerator method that returns an IEnumerator object. It's the IEnumerator object that allows a foreach-in to work the way it does.

The new iterator pattern makes use of the yield keyword. Using yield eliminates the need for you to have to actually create and implement an IEnumerator object. It does it for you. yield handles all of the storage and retrieval operations that come with GetEnumerator().

The Old Way of Creating Enumerable Classes: public class MyOldEnumerableClass { private String[] m_MyStrings; public MyOldEnumerableClass() { this.m_MyStrings = new String[1000000]; for (int i = 0; i <= 999999; i ++) this.m_MyStrings[i] = "Hello!"; } public System.Collections.IEnumerator GetEnumerator() { // m_MyStrings.GetEnumerator() does all of the work: return this.m_MyStrings.GetEnumerator(); } } The New Way of Creating Enumerable Classes: public class MyNewEnumerableClass { private String[] m_MyStrings; public MyNewEnumerableClass() { this.m_MyStrings = new String[1000000]; for (int i = 0; i <= 999999; i++) this.m_MyStrings[i] = "Hello!"; } public System.Collections.IEnumerator GetEnumerator() { // yield does all of the work: foreach (String str in this.m_MyStrings) yield return str; } }

C# 2.0 Iterators are Faster than C# 1.0 and 1.1 Counterparts

I performed a test by storing 1,000,000 string objects in two different classes and then retrieving the contents of each via a foreach-in loop. The new iterator pattern in C# 2.0 was significantly faster than its C# 1.0 and 1.1 counterparts: the new iterator took 93 milliseconds to complete a foreach-in operation while the older iterator took 343 milliseconds.

Here's the code I used to perform this test: namespace WhatsNew20Iterators { class Program { static void Main(string[] args) { MyOldEnumerableClass myOld = new MyOldEnumerableClass(); DateTime dtOld = DateTime.Now; foreach (String str in myOld) { // Do nothing... continue... } TimeSpan tsOld = DateTime.Now.Subtract(dtOld); MyNewEnumerableClass myNew = new MyNewEnumerableClass(); DateTime dtNew = DateTime.Now; foreach (String str in myNew) { // Do nothing... continue... } TimeSpan tsNew = DateTime.Now.Subtract(dtNew); Console.WriteLine("Old Method took {0} milliseconds.", tsOld.Milliseconds.ToString()); Console.WriteLine("New Method took {0} milliseconds.", tsNew.Milliseconds.ToString()); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } public class MyOldEnumerableClass { private String[] m_MyStrings; public MyOldEnumerableClass() { this.m_MyStrings = new String[1000000]; for (int i = 0; i <= 999999; i ++) this.m_MyStrings[i] = "Hello!"; } public System.Collections.IEnumerator GetEnumerator() { // m_MyStrings.GetEnumerator() does all of the work: return this.m_MyStrings.GetEnumerator(); } } public class MyNewEnumerableClass { private String[] m_MyStrings; public MyNewEnumerableClass() { this.m_MyStrings = new String[1000000]; for (int i = 0; i <= 999999; i++) this.m_MyStrings[i] = "Hello!"; } public System.Collections.IEnumerator GetEnumerator() { // yield does all of the work: foreach (String str in this.m_MyStrings) yield return str; } } } }

Comments Click Here to Add your Comment
No Comments Found.
 
COPYRIGHT/DISCLAIMER | CONTACT US