Passing Integer value into GetEnumerator method in ArrayList

In this article I will explain how to pass Pass Integer value into the GetEnumerator(Int32,Int32) method of ArrayList.
  • 2138

Introduction

The GetEnumerator(Int32,Int32) method of ArrayList returns an enumerator for a range of elements in the ArrayList.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.The first argument of the GetEnumerator method is the index location from where we want to display the element and the second element is the number of element that we want to display.

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

            //display the elements staring from the index location 2 and print 3 elements onwords.

            IEnumerator e2 = days.GetEnumerator(2, 3);

            while (e2.MoveNext())

            {

                Object obj = e2.Current;

                Console.WriteLine(obj);

            }

        }

    }

}

 

Output

getenumerator1.jpg 

 

Ask Your Question  

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

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.