Array.GetEnumerator 方法

返回 ArrayIEnumerator

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Function GetEnumerator As IEnumerator
用法
Dim instance As Array
Dim returnValue As IEnumerator

returnValue = instance.GetEnumerator
public IEnumerator GetEnumerator ()
public:
virtual IEnumerator^ GetEnumerator () sealed
public final IEnumerator GetEnumerator ()
public final function GetEnumerator () : IEnumerator

返回值

用于 ArrayIEnumerator

备注

C# 语言的 foreach 语句(在 C++ 中为 for each,在 Visual Basic 中为 For Each)隐藏了枚举数的复杂性。因此,建议使用 foreach,而不直接操作枚举数。

枚举数可用于读取集合中的数据,但不能用于修改基础集合。

最初,枚举数被定位于集合中第一个元素的前面。Reset 也将枚举数返回到此位置。在此位置上,未定义 Current。因此,在读取 Current 的值之前,必须调用 MoveNext 将枚举数提前到集合的第一个元素。

在调用 MoveNextReset 之前,Current 返回同一对象。MoveNextCurrent 设置为下一个元素。

如果 MoveNext 越过集合的末尾,枚举数就会被放置在此集合中最后一个元素的后面,且 MoveNext 返回 false。当枚举数位于此位置时,对 MoveNext 的后续调用也返回 false。如果对 MoveNext 的最近一次调用返回 false,则说明没有定义 Current。若要再次将 Current 设置为集合的第一个元素,可以调用 Reset,然后再调用 MoveNext

只要集合保持不变,枚举数就保持有效。如果对集合进行更改(如添加、修改或删除元素),则枚举数将失效且不可恢复,而且其行为是不确定的。

该枚举数不具有独占访问集合的权限;因此,枚举整个集合本质上不是一个线程安全的过程。若要确保枚举过程中的线程安全,可以在整个枚举过程中锁定集合。若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。

此方法是 O(1) 操作。

示例

下面的代码示例显示如何使用 GetEnumerator 列出数组的元素。

Imports System

Public Class SamplesArray

   Public Shared Sub Main()

      ' Creates and initializes a new Array.
      Dim myArr(10) As [String]
      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.
      Dim i As Integer = 0
      Dim myEnumerator As System.Collections.IEnumerator = myArr.GetEnumerator()
      Console.WriteLine("The Array contains the following values:")
      While myEnumerator.MoveNext() And Not (myEnumerator.Current Is Nothing)
         Console.WriteLine("[{0}] {1}", i, myEnumerator.Current)
         i += 1
      End While 

   End Sub 'Main

End Class 'SamplesArray 


'This code produces the following output.
'
'The Array contains the following values:
'[0] The
'[1] quick
'[2] brown
'[3] fox
'[4] jumped
'[5] over
'[6] the
'[7] lazy
'[8] dog
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();
      Console.WriteLine( "The Array contains the following values:" );
      while (( myEnumerator.MoveNext() ) && ( myEnumerator.Current != null ))
         Console.WriteLine( "[{0}] {1}", i++, myEnumerator.Current );

   }
 
}


/* 
This code produces the following output.

The Array contains the following values:
[0] The
[1] quick
[2] brown
[3] fox
[4] jumped
[5] over
[6] the
[7] lazy
[8] dog

*/
using namespace System;

int main()
{
   // Creates and initializes a new Array.
   array<String^>^myArr = gcnew array<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();
   Console::WriteLine( "The Array contains the following values:" );
   while ( (myEnumerator->MoveNext()) && (myEnumerator->Current != nullptr) )
      Console::WriteLine( "[{0}] {1}", i++, myEnumerator->Current );
}

/* 
This code produces the following output.

The Array contains the following values:
[0] The
[1] quick
[2] brown
[3] fox
[4] jumped
[5] over
[6] the
[7] lazy
[8] dog

*/
import System.*;

public class SamplesArray
{
    public static void main(String[] args)
    {
        // Creates and initializes a new Array.
        String myArr[] = new String[10];

        myArr.set_Item(0, "The");
        myArr.set_Item(1, "quick");
        myArr.set_Item(2, "brown");
        myArr.set_Item(3, "fox");
        myArr.set_Item(4, "jumped");
        myArr.set_Item(5, "over");
        myArr.set_Item(6, "the");
        myArr.set_Item(7, "lazy");
        myArr.set_Item(8, "dog");

        // Displays the values of the Array.
        int i = 0;
        System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();

        Console.WriteLine("The Array contains the following values:");
        while ((myEnumerator.MoveNext() && myEnumerator.get_Current() != null)) {
            Console.WriteLine("[{0}] {1}", System.Convert.ToString(i++), 
                myEnumerator.get_Current());
        }
    } //main
} //SamplesArray
 /* 
This code produces the following output.

The Array contains the following values:
[0] The
[1] quick
[2] brown
[3] fox
[4] jumped
[5] over
[6] the
[7] lazy
[8] dog

*/

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Array 类
Array 成员
System 命名空间