Thursday, June 9, 2011

IEnumerator & IEnumerable

Enumerators are helpful in iterating through data. IEnumerator and IEnumerable both exists in System.Collections and works only on non-generic collections. All collections like ArrayList are created by implementing IEnumerable.

With the help of both Interfaces we can create our own collection.
IEnumerable

IEnumerable interface have only one method GetEnumerator() that returns IEnumerator.
1. public IEnumerator GetEnumerator();

IEnumerator

IEnumerator interface have 2 methods and 1 property:
1. void Reset()              --> Method
2. bool MoveNext()      --> Method
3. object Current           --> Property
 
Rest()
Void Rest(), this method resets current location to start point and sets rese value to "-1".
public void Reset()                                          
{                                                                     
    iIndex=-1;                                                   
}                                                                     
 
MoveNext()
bool MoveNext(), this method helps in taking you to the required location in collection and in response returns bool flag value. If MoveNext() method get any value on current location then it will return true otherwise it will return false.

public bool MoveNext()                                  
{                                                                     
    return(++iIndex);                                        
}                                                                    

Current
object Current, this property return element of the collection by specifying location.

public object Current                                       
{                                                                     
    get                                                              
    {                                                                 
         return(Orders[iIndex]);                          
    }                                                                
}                                                                     

No comments:

Post a Comment