IEnumerable

An IEnumerable generic interface is returned from query expressions. A query expression that selects ints will be of type 


Enumerable, IEnumerable<T>The only thing you want is to iterate over the elements in a collection. You only need read-only access to that collection.
IEnumerable interface contains an abstract member function called GetEnumerator() and return an interfaceIEnumerator on any success call. This IEnumerator interface will allow us to iterate through any custom collection.

any element in a collection can be retrieved through its index property. But instead of element index, the IEnumerator provides two abstract methods and a property to pull a particular element in a collection. And they are Reset()MoveNext() and Current.

  1. IEnumerable can move forward only over a collection, it can’t move backward and between the items.
  1. IEnumerable is best to query data from in-memory collections like List, Array etc.
using System;
using System.Text;
using System.Collections;

namespace TestDomain
{
        class Program
        {
            public static void Main(String[] args)
            {
                List<string> List = new List<string>();
                List.Add("Ravi");
                List.Add("jaideep");
                List.Add("shyam");
                List.Add("Sachin");

                IEnumerable names = from n in List where (n.StartsWith("S")) select n;

                foreach (string name in names)
                {
                    Console.WriteLine(name);
                }

                Console.ReadLine();
            }
        }
}

Enumerators are used to read data in the collection. The foreach statement hides the complexity of the enumerators, but you can directly manipulate IEnumerator for customized iterations. Let's do an example:

Code:
List<string> myList = new List<string>();for (int i = 0; i < 10; i++)
{
    myList.Add("Item " + i.ToString());
}
IEnumerator<string> myEnum = myList.GetEnumerator();
 
myEnum.Reset();            
myEnum.MoveNext();
myEnum.MoveNext();
myEnum.MoveNext();
System.Console.WriteLine(myEnum.Current);
myEnum.MoveNext();
myEnum.MoveNext();
System.Console.WriteLine(myEnum.Current);
myEnum.Reset();
myEnum.MoveNext();
System.Console.WriteLine(myEnum.Current);

Output:
Item 2
Item 4
Item 0 

In order to reach the first element, you should run MoveNext method of Enumerator. Initial Position of Enumerator does not point the first element.

Implementing IEnumerable and IEnumerator on your custom objects

IEnumerable interface should be implemented in order to use your custom objects in the form of a collection (series of values or objects). Which means you can use it directly with the foreach statement. IEnumerable interface has a method called GetEnumerator() which returns an object implemented IEnumerator. Let's do an example: PowersOfTwo class implements IEnumerable so any instance of this class can be accessed as a collection. 
class PowersOfTwo : IEnumerable<int
{       
    public IEnumerator<int> GetEnumerator()
    {
        return new PowersOfTwoEnumerator();
    }        
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }        
}
Test:PowersOfTwo p2 = new PowersOfTwo();foreach (int p in p2)
{
    System.Console.WriteLine(p);
}


Output:

2 4 8 16 32 64 128 256 512 1024


Actually the magic trick lies in the PowersOfTwoEnumerator Class
    class PowersOfTwoEnumerator : IEnumerator<int>
    {
        private int index = 0;
        public int Current
        {
            get { return (int)System.Math.Pow(2, index); }
        }
 
        object System.Collections.IEnumerator.Current
        {
            get { return Current; }
        }
        public bool MoveNext()
        {
            index++;
            if (index > 10)
                return false;
            else                return true;
        }
        public void Reset()
        {
            index = 0;
        }
        public void Dispose()
        {
        }
    }


Current returns the same element until MoveNext is called. Initial index is zero each MoveNext method increments the index by 1 up to 10 then it returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false. 
As general rules:
If the last call to MoveNext returned false, Current is undefined. You cannot set Current to the first element of the collection again; you must create a new enumerator instance instead.

Using The Yield Keyword

yield keyword is introduced by C# 2.0 in order to simplify implementation iterator pattern in your custom objects. 
public IEnumerable<int> GetPowersofTwo()
{
   for (int i = 1; i < 10; i++)       yield return (int)System.Math.Pow(2, i);   yield break;
}


Yield is not a feature of the .Net runtime. It is just a C# language feature. During compilation Compiler converts yield method and its class to instances of IEnumerable and IEnumerator implemented classes. 


Please note :  Data has been taken from various site 

More reference please use this :http://www.codeproject.com/Articles/832189/List-vs-IEnumerable-vs-IQueryable-vs-ICollection-v
































































Comments

Popular posts from this blog

Prime Number Program in C#

Fibonacci Series in C#

view in sql server