C# 2.0 Features - Part 2.4 Iterators

Mar
15
2010
In Categories: .NET | CLR
Tags | |

You can see part 1 here, part 2.1 here, part 2.2 here and part 2.3 here

It is not a new feature as well in C#, Although .NET framework adds keyword to provide extra functionality. There are iterators in C++, C++ iterator is the C# enumerator. Anyway, this is not about C++ let's see what are the C# iterators and what are these keywords.

Iterators provides is a method, get accessor, or operator that performs a custom iteration over an array or collection class without implementing the IEnumerator and IEnumerable interfaces (or their generics interfaces IEnumerable(T) and IEnumerator(T)). Iterators are like read-only, forward-only pointers. To satisfy this feature we use yield keyword.

Let's see some code to make it more clear

    

    1 using System.Collections;

    2 

    3 public class SomeArabCountries : IEnumerable

    4 {

    5     string[] ArabCountries = { "Egypt", "UAE", "KSA" };

    6 

    7     public IEnumerator GetEnumerator()

    8     {

    9         for (int i = 0; i < ArabCountries.Length; i++)

   10         {

   11             yield return ArabCountries[i];

   12         }

   13     }

   14 }

   15 

   16 class TheArabDefinedCountiresInTheSystem

   17 {

   18     static void Main()

   19     {

   20         SomeArabCountries arabCountries =

   21             new SomeArabCountries();

   22 

   23         foreach (string country in arabCountries)

   24         {

   25             System.Console.Write(country + " ");

   26         }

   27     }

   28 }

As we can see in the above code we only inherit from "IEnumerable" and implement the "GetEnumerator" function using "yield" keyword to return the elements. If we don't have "yeild" what should we do? Let's see the same code without the "yiled" keyword.

    1 using System;

    2 using System.Collections;

    3 

    4 public class SomeArabCountries : IEnumerable

    5 {

    6 

    7     string[] ArabCountries = { "Egypt", "UAE", "KSA" };

    8 

    9 

   10     IEnumerator IEnumerable.GetEnumerator()

   11     {

   12         return new ArabCountriesEnum(ArabCountries);

   13     }

   14 }

   15 

   16 public class ArabCountriesEnum : IEnumerator

   17 {

   18     public string[] countries;

   19 

   20     int position = -1;

   21 

   22     public ArabCountriesEnum(string[] list)

   23     {

   24         countries = list;

   25     }

   26 

   27     public bool MoveNext()

   28     {

   29         position++;

   30         return (position < countries.Length);

   31     }

   32 

   33     public void Reset()

   34     {

   35         position = -1;

   36     }

   37 

   38     public object Current

   39     {

   40         get

   41         {

   42             try

   43             {

   44                 return countries[position];

   45             }

   46             catch (IndexOutOfRangeException)

   47             {

   48                 throw new InvalidOperationException();

   49             }

   50         }

   51     }

   52 }

   53 

   54 class TheArabDefinedCountiresInTheSystem

   55 {

   56     static void Main()

   57     {

   58         SomeArabCountries arabCountries =

   59             new SomeArabCountries();

   60 

   61         foreach (string country in arabCountries)

   62         {

   63             Console.Write(country + " ");

   64         }

   65     }

   66 }

As you can see without "yeild" we have to implement the "IEnumerator" interface and add a lot of logic that may introduce bugs. So simply "yield" keyword has replaced a complete class.

Comments

trackback
Technical Architect blog
5/22/2010 3:22:00 AM Permalink

C# 2.0 Features - Part 2.5 Partial classes

C# 2.0 Features - Part 2.5 Partial classes

Comments are closed