Labels

Monday, October 6, 2008

C# 2.0 - Iterators

Hi,

This blog post summarizes the new features of C# 2.0.

Below are the new features been introduced.

§ Partial Classes

§ Static Classes

§ Generic Classes

§ Anonymous methods

§ Iterators

§ Nullable Types

Iterators

- Iterators provide a simpler way to create classes that can be used with the foreach statement without implementing the IEnumerable and IEnumerator interfaces.

- The C# foreach statement is used to iterate over the elements of an enumerable collection. In order to be enumerable, a collection must have a parameterless GetEnumerator method that returns an enumerator. Generally, enumerators are difficult to implement, but the task is significantly simplified with iterators.

- An iterator is a method, get accessor or operator that enables you to support foreach iteration in a class or struct without having to implement the entire IEnumerable interface. Instead, you provide just an iterator, which simply traverses the data structures in your class. When the compiler detects your iterator, it will automatically generate the Current, MoveNext and Dispose methods of the IEnumerable or IEnumerator interface.

- Iterators are especially useful with collection classes, providing an easy way to iterate non-trivial data structures such as binary trees.

- An iterator is a section of code that returns an ordered sequence of values of the same type.

- An iterator can be used as the body of a method, an operator, or a get accessor.

- The iterator code uses the yield return statement to return each element in turn. yield break ends the iteration.

- The yield return statement produces the next value of the iteration.

- The yield break statement indicates that the iteration is complete.

- Multiple iterators can be implemented on a class. Each iterator must have a unique name just like any class member, and can be invoked by client code in a foreach statement as follows:

foreach(int x in SampleClass.Iterator2){}

- The return type of an iterator must be IEnumerable or IEnumerator.

- The yield keyword is used to specify the value, or values, returned. When the yield return statement is reached, the current location is stored. Execution is restarted from this location the next time the iterator is called.

- A member implemented via an iterator may be overridden or overloaded by other members that may or may not be implemented with iterators.

Imp – Code 2.1 & 2.2

- The foreach statement implicitly calls a collection’s parameterless GetEnumerator method to obtain an enumerator. There can only be one such parameterless GetEnumerator method defined by a collection, yet it is often appropriate to have multiple ways of enumerating and ways of controlling the enumeration through parameters. In such cases, a collection can use iterators to implement properties (TopToBottom & BottomToTop) or methods (FromToBuy) that return one of the enumerable interfaces. For e.g. TopToBottom and BottomToTop, of type IEnumerable<T>. See Code 2.

Code 1 –

 
class Product
{
    private string _name;
    public string ProductName
    {
        get { return _name; }
        set { _name = value; }
    }
 
    public Product(string name)
    {
        _name = name;
    }
 
    public Product(){}
}
 
 

Code 1.1 –

 
class ProductCategory
{
    private List<Product> _products;
    private string _name;
    public string CategoryName
    {
        get { return _name; }
        set { _name = value; }
    }
 
    public ProductCaltegory(string name) : this()
    {
        _name = name;
    }
 
    public ProductCaltegory()
    {
        _products = new List<Product>(5);
    }
 
    public void AddProduct(Product p)
    {
        _products.Add(p);
    }
 
    public IEnumerator GetEnumerator()
    {
        foreach (Product p in _products)
            yield return p;
    }
}

Code 1.2 -

 
class Program
{
    static void Main(string[] args)
    {
        Product p1 = new Product("Product A");
        Product p2 = new Product("Product B");
        Product p3 = new Product("Product C");
        Product p4 = new Product("Product D");
 
        ProductCaltegory category = new ProductCaltegory("Main Category");
 
        category.AddProduct(p1);
        category.AddProduct(p2);
        category.AddProduct(p3);
        category.AddProduct(p4);
 
        foreach (Product p in category)
        {
            Console.WriteLine(p.ProductName);
        }
    }
}

Code 1.1 –

 
class ProductCategory 
{
    private List<Product> _products;
    private string _name;
    public string CategoryName
    {
        get { return _name; }
        set { _name = value; }
    }
 
    public ProductCaltegory(string name) : this()
    {
        _name = name;
    }
 
    public ProductCaltegory()
    {
        _products = new List<Product>(5);
    }
 
    public void AddProduct(Product p)
    {
        _products.Add(p);
    }
 
    public IEnumerator GetEnumerator()
    {
        foreach (Product p in _products)
            yield return p;
    }
}

Code 2.1 –

 
 
public IEnumerator GetEnumerator()
    {
        foreach (Product p in _products)
            yield return p;
    }
 
 
public IEnumerable<T> TopToBottom 
{
   get 
   {
      return this; 
   }
}
 
 
public IEnumerable<T> BottomToTop 
{
   get 
   {
         for (int i = count; i > 0; i--) 
         {
            yield return items[i];
         }
   }
}
 
public IEnumerable<T> FromToBuy (int from, int to, int by) 
{
   for (int i = from; i > by; i++) 
         {
            yield return items[i];
         }
}
 

Code 2.2 -

 
class Program
{
    static void Main(string[] args)
    {
        Product p1 = new Product("Product A");
        Product p2 = new Product("Product B");
        Product p3 = new Product("Product C");
        Product p4 = new Product("Product D");
 
        ProductCaltegory category = new ProductCaltegory("Main Category");
 
        category.AddProduct(p1);
        category.AddProduct(p2);
        category.AddProduct(p3);
        category.AddProduct(p4);
 
        foreach (Product p in category.TopToBottom)
        {
            Console.WriteLine(p.ProductName);
        }
 
        foreach (Product p in category. BottomToTop)
        {
            Console.WriteLine(p.ProductName);
        }
 
 
       foreach (Product p in FromToBuy(1,5))
        {
            Console.WriteLine(p.ProductName);
        }
 
 
 
    }

}

Hope this helps

Thanks & Regards,

Arun Manglick || Senior Tech Lead

No comments:

Post a Comment