Translate

Monday, October 22, 2012

IEnumerable of T tutorial

Why implement IEnumerable<T>?

If you want to use Linq on an instance of a class, it must implement the interface IEnumerable<T>.


What are the methods that need to be implemented?

 IEnumerable<T>:
 namespace System.Collections.Generic
{

    public interface IEnumerable<T> : IEnumerable    
    {   
        IEnumerator<T> GetEnumerator();
    }
}
Since IEnumerable<T> inherits from IEnumerable, you must implement it too.


 IEnumerable:
namespace System.Collections
{  
    public interface IEnumerable   

   {      
        IEnumerator GetEnumerator();
    }
}


Check this article for an easy introduction to the non generic version IEnumerable.


Implementing the above two interfaces cannot be achieved without implementing these interfaces below


IEnumerator<T>:
namespace System.Collections.Generic
{
    public interface IEnumerator<T> : IDisposableIEnumerator  

    {     
        T Current { get; }
    }
}


IEnumerator:
namespace System.Collections

    public interface IEnumerator   

   {      
        object Current { get; }  
        bool MoveNext(); 
        void Reset();
    }
}


IDisposable:
namespace System

    public interface IDisposable  

   {     
        void Dispose();
    }
}


Okay, now give me an example that shows an IEnumerable<T> implementation

In the example below, the class BookEnumerable implements IEnumerable<T>. Hence you can apply LINQ on an instance of that class.


using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
 
 
namespace ConsoleApplication4
{
 
    class Program
    {
        static void Main(string[] args)
        {
            BookEnumerable books = new BookEnumerable();
 
            var x = from b in books
                    where b.cost > 10
                    select b;
 
            foreach (var b in x.ToList())
            {
                Console.WriteLine(b.title);
            }
 
        }
    }
 
    public class Book
    {
        public string title;
        public int cost;
    }
 
    class BookEnumerable : IEnumerable<Book>
    {
        IEnumerator<Book> er = new BookEnumerator();
 
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
 
        public IEnumerator<Book> GetEnumerator()
        {
            return er;
        }
    }
 
 
    class BookEnumerator : IEnumerator<Book>
    {
 
        Book[] books = Utilities.GetBookArray();
 
        int position = -1;
 
        public Book Current
        {
            get
            {
                return books[position];
            }
        }
 
        object IEnumerator.Current
        {
            get
            {
                return Current;

            }
        }
 
        public bool MoveNext()
        {
 
            position++;
            if (position < books.Length)
            {
                return true;//this means a next item exists
            }
            else
            {
                return false;//this means there are no more items to move further to
            }
        }
 
        public void Reset()
        {
            position = -1;
        }
 
        public void Dispose() { }
 
 
    }
 
    public class Utilities
    {
        public static Book[] GetBookArray()
        {
            Book b1 = new Book();
            b1.title = "Code Complete";
            b1.cost = 28;
 
            Book b2 = new Book();
            b2.title = "The Pragmatic Programmer";
            b2.cost = 33;
 
            return new Book[] { b1, b2 };
 
        }
    }
}

No comments:

Post a Comment

Comments will appear once they have been approved by the moderator