CopyPastehas never been so tasty!

IEnumerator Interface with ArrayList in C#

by anonymous

  • 0
  • 0
  • 0
95 views

Orignal Source:http://codingvilla.com/ienumerator-interface-arraylist-csharp-single-article-560.aspx

IEnumerator Interface:IEnumerator belongs to the Ssystem.Collections namespace. It provides simple iteration over non generics collection. You can read the data using IEnumerator in Collection, but you can’t use it to modify the underlying data. It has one property and two methods, which exposes some functionality. Initially the enumerator is position before the first element in the collections. It has a reset method, which brings the enumerator to that position. If you call current at this position, it will definitely throw an exception.The move next method takes the enumerator to the first element in the collections, and then you can call the current method to read the value.The enumerator remains valid until no modifications are made to the collections. If any modification such as adding, modifying and deleting are performed then there will a exception “InvalidOperationException”. So make sure no changes are made while using Move Next and Current method.Current Method:It returns the current element in the collections. But make sure you have called Move Next method before calling that method.Move Next Method:It advances the enumerator to the position of first element in the collection. As we discussed before enumerator resides before first element initially.Reset: This method can be used to reset the position of enumerator to its initial position, which was before the first element in the collection.ArrayList:ArrayList is a collection whose size can dynamically increases with use. There is no guarantee that ArrayList is sorted. You must perform BinarySearch before performing operation on the ArrayList. It has huge number of useful methods. You can trim the size of the ArrayList too by calling TrimToSize or capacity property explicitly. It allows duplicated value. You can’t use multidimensional element in ArrayList as they are not supported.Example:In this simple console application we will demonstrate use of both the IEnumerator interface and ArrayList in c#.Code: 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

 namespace IEnumeratorAndArrayList

{

   class Program

    {

       static void Main(string[] args)

        {

            // Array of natural numbers

            int[] naturalNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

             // a new ArrayList initially of size 1

            ArrayList list = new ArrayList(1);

             // to iterate in the naturalNumbers array

            foreach (int value in naturalNumbers)

            {

                // add element to the list

                list.Add(value);

                  }

             // gets the enumerator to access the list

            IEnumerator ienumratorObj = list.GetEnumerator();

             // first it moves next to first element, we have used the MoveNext() method here

            while (ienumratorObj.MoveNext())

            {

                // Display the elements

                Console.WriteLine("numbers  = {0}", ienumratorObj.Current);

            }

             // Lets reset the position

            Console.WriteLine("\nAfter reset method\n");

             // resets to the intial position, position before first element.

            ienumratorObj.Reset();

             // int i = (int)ienumratorObj.Current; Exception: numeration has not started. Call MoveNext.

        // moves to the next position, such as to the first element otherwise exception.

    ienumratorObj.MoveNext();

  Console.WriteLine("First Element = {0}", ienumratorObj.Current);

            // displays remaining values

      

 while (ienumratorObj.MoveNext())

            {

                Console.WriteLine("Values = {0}", ienumratorObj.Current);

            }

             Console.ReadKey();

         }

    }

}

Add A Comment: