Array.GetEnumerator メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
IEnumeratorのArrayを返します。
public:
virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator();
public virtual System.Collections.IEnumerator GetEnumerator();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator
Public Overridable Function GetEnumerator () As IEnumerator
返品
実装
例
次のコード例は、 GetEnumerator を使用して配列の要素を一覧表示する方法を示しています。
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] = "jumps";
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] jumps
[5] over
[6] the
[7] lazy
[8] dog
*/
// Creates and initializes a new Array.
let myArr = Array.zeroCreate 10
myArr[0..8] <-
[| "The"
"quick"
"brown"
"fox"
"jumps"
"over"
"the"
"lazy"
"dog" |]
// Displays the values of the Array.
let mutable i = 0
let myEnumerator = myArr.GetEnumerator()
printfn "The Array contains the following values:"
while myEnumerator.MoveNext() && myEnumerator.Current <> null do
printfn $"[{i}] {myEnumerator.Current}"
i <- i + 1
// This code produces the following output.
// The Array contains the following values:
// [0] The
// [1] quick
// [2] brown
// [3] fox
// [4] jumps
// [5] over
// [6] the
// [7] lazy
// [8] dog
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) = "jumps"
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
End Class
'This code produces the following output.
'
'The Array contains the following values:
'[0] The
'[1] quick
'[2] brown
'[3] fox
'[4] jumps
'[5] over
'[6] the
'[7] lazy
'[8] dog
注釈
C# 言語の foreach ステートメント (Visual Basic の For Each) は、列挙子の複雑さを隠します。 したがって、列挙子を直接操作するのではなく、 foreach を使用することをお勧めします。
列挙子を使用してコレクション内のデータを読み取ることができますが、基になるコレクションを変更するために使用することはできません。
最初は、列挙子はコレクション内の最初の要素の前に配置されます。 Reset また、列挙子をこの位置に戻します。 この位置では、 Current は未定義です。 したがって、MoveNextの値を読み取る前に、Currentを呼び出して列挙子をコレクションの最初の要素に進める必要があります。
Current は、 MoveNext または Reset が呼び出されるまで、同じオブジェクトを返します。 MoveNext は、 Current を次の要素に設定します。
MoveNextコレクションの末尾を渡すと、列挙子はコレクション内の最後の要素の後に配置され、MoveNextはfalseを返します。 列挙子がこの位置にある場合、後続の MoveNext の呼び出しでも falseが返されます。
MoveNext
false最後の呼び出しが返された場合、Currentは未定義です。
Currentをコレクションの最初の要素に再度設定するには、Resetを呼び出し、その後にMoveNextを呼び出します。
列挙子は、コレクションが変更されない限り有効なままです。 要素の追加、変更、削除など、コレクションに変更が加えられた場合、列挙子は回復不能に無効になり、その動作は未定義になります。
列挙子は、コレクションへの排他的アクセス権を持っていません。したがって、コレクションを通じた列挙は、本質的にスレッド セーフなプロシージャではありません。 列挙中のスレッド セーフを保証するために、列挙体全体の間にコレクションをロックできます。 読み取りと書き込みのためにコレクションに複数のスレッドからアクセスできるようにするには、独自の同期を実装する必要があります。
このメソッドは O(1) 操作です。