Click here to Skip to main content
16,004,924 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Please Answer With Simple Sample How To Use and implement From IEnumrable And IEnumrator
Thank
Posted

 
Share this answer
 
Here's a simple code sample that shows how to build your own enumerable collection
C#
public class MyClass
{
    // ...
}

public class MyClassCollection : IEnumerator, IEnumerator<MyClass>, IEnumerable, IEnumerable<MyClass>
{
    private int index;
    private List<MyClass> collection;

    public MyClassCollection()
    {
        collection = new List<MyClass>();
    }
    public MyClassCollection(IEnumerable<MyClass> items)
    {
        index = -1;
        collection = new List<MyClass>(items);
    }

    public MyClass this[int index]
    {
        get { return collection[index]; }
        set { collection[index] = value; }
    }

    public void Add(MyClass item)
    {
        collection.Add(item);
    }
    MyClass IEnumerator<MyClass>.Current
    {
        get
        {
            if (index == -1)
                return null;
            else
                return collection[index];
        }
    }
    void IDisposable.Dispose()
    {
        collection = null;
    }
    object IEnumerator.Current
    {
        get
        {
            if (index == -1)
                return null;
            else
                return collection[index];
        }
    }
    bool IEnumerator.MoveNext()
    {
        index++;
        return index >= collection.Count;
    }
    void IEnumerator.Reset()
    {
        index = -1;
    }
    IEnumerator<MyClass> IEnumerable<MyClass>.GetEnumerator()
    {
        return (collection as IEnumerable<MyClass>).GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return (collection as IEnumerable).GetEnumerator();
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900