C# Array GetEnumerator

Description

Array GetEnumerator returns an IEnumerator for the Array.

Syntax

Array.GetEnumerator has the following syntax.


public IEnumerator GetEnumerator()

Returns

Array.GetEnumerator method returns An IEnumerator for the Array.

Example

The following code example shows how to use GetEnumerator to list the elements of an array.


//w  w  w.jav  a 2s.  c om
using System;

public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array.
      String[] myArr = new String[10];
      myArr[0] = "The";
      myArr[1] = "quick";
      myArr[2] = "brown";
      myArr[3] = "fox";
      myArr[4] = "jumped";
      myArr[5] = "over";
      myArr[6] = "the";
      myArr[7] = "lazy";
      myArr[8] = "dog";

      // Displays the values of the Array. 
      int i = 0;
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      while (( myEnumerator.MoveNext() ) && ( myEnumerator.Current != null )){
         Console.WriteLine( "[{0}] {1}", i++, myEnumerator.Current );
      }

   }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version