GetEnumerator method in ArrayList

In this article I will explain the GetEnumerator method of ArrayList.
  • 4080

Introduction

The GetEnumerator method of ArrayList returns an enumerator for the entireArrayList.Initially, the enumerator is positioned before the first element in the collection,so in order to move the next element we use MoveNext method of enumerator.

Example

namespace ConsoleApplication6

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the arraylist named days

            ArrayList days = new ArrayList();

            //add the elements in the arraylist

            days.Add("Sunday");

            days.Add("Monday");

            days.Add("Tuesday");

            days.Add("Wednesday");

            days.Add("Thusday");

            days.Add("Friday");

            days.Add("Saturday");

            //use of GetEnumerator method

            IEnumerator e = days.GetEnumerator();

            while (e.MoveNext())

            {

                Object obj = e.Current;

                Console.WriteLine(obj);

            }

            Console.WriteLine();

        }

    }

}

 

Output

getenumerator.jpg 

 

Ask Your Question  

Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.