Sunday, 10 January 2016

Using the GetEnumerator() method to get an enumerator

using System;
using System.Collections;

class MainClass
{
    public static void Main()
    {
        ArrayList myArrayList = new ArrayList();
        myArrayList.Add("s");
        myArrayList.Add("s");
        myArrayList.Add("s");
        myArrayList.Add("s");
        myArrayList.Add("s");
        myArrayList.Add("s");
        myArrayList.Add("s");
        myArrayList.Add("s");
        myArrayList.Add("s");
        myArrayList.Add("s");


        string[] anotherStringArray = { "Hello", "some", "more", "text" };
        myArrayList.SetRange(0, anotherStringArray);

        Console.WriteLine("Using the GetEnumerator() method to get an enumerator");
        IEnumerator myEnumerator = myArrayList.GetEnumerator();
        while (myEnumerator.MoveNext())
        {
            Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
        }
        myEnumerator.Reset();
        myEnumerator.MoveNext();
        Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);

    }
}

---------------
OUTPUT:

Using the GetEnumerator() method to get an enumerator
myEnumerator.Current = Hello
myEnumerator.Current = some
myEnumerator.Current = more
myEnumerator.Current = text
myEnumerator.Current = s
myEnumerator.Current = s
myEnumerator.Current = s
myEnumerator.Current = s
myEnumerator.Current = s
myEnumerator.Current = s
myEnumerator.Current = Hello
-------------

1 comment: