IEnumerator<T> インターフェイス

定義

ジェネリック コレクションに対する単純な反復処理をサポートします。

generic <typename T>
public interface class IEnumerator : IDisposable, System::Collections::IEnumerator
public interface IEnumerator<out T> : IDisposable, System.Collections.IEnumerator
public interface IEnumerator<T> : IDisposable, System.Collections.IEnumerator
type IEnumerator<'T> = interface
    interface IEnumerator
    interface IDisposable
type IEnumerator<'T> = interface
    interface IDisposable
    interface IEnumerator
Public Interface IEnumerator(Of Out T)
Implements IDisposable, IEnumerator
Public Interface IEnumerator(Of T)
Implements IDisposable, IEnumerator

型パラメーター

T

列挙するオブジェクトの型。

この型パラメーターは共変です。 つまり、指定した型、または強い派生型のいずれかを使用することができます。 共変性および反変性の詳細については、「ジェネリックの共変性と反変性」をご覧ください。
派生
実装

次の例は、カスタム オブジェクトの IEnumerator<T> コレクション クラスの インターフェイスの実装を示しています。 カスタム オブジェクトは 型 Boxのインスタンスであり、コレクション クラスは です BoxCollection。 このコード例は、 インターフェイス用に提供されるより大きな例の ICollection<T> 一部です。


// Defines the enumerator for the Boxes collection.
// (Some prefer this class nested in the collection class.)
public class BoxEnumerator : IEnumerator<Box>
{
    private BoxCollection _collection;
    private int curIndex;
    private Box curBox;

    public BoxEnumerator(BoxCollection collection)
    {
        _collection = collection;
        curIndex = -1;
        curBox = default(Box);
    }

    public bool MoveNext()
    {
        //Avoids going beyond the end of the collection.
        if (++curIndex >= _collection.Count)
        {
            return false;
        }
        else
        {
            // Set current box to next item in collection.
            curBox = _collection[curIndex];
        }
        return true;
    }

    public void Reset() { curIndex = -1; }

    void IDisposable.Dispose() { }

    public Box Current
    {
        get { return curBox; }
    }

    object IEnumerator.Current
    {
        get { return Current; }
    }
}
' Defines the enumerator for the Boxes collection.
' (Some prefer this class nested in the collection class.)
Public Class BoxEnumerator
    Implements IEnumerator(Of Box)
    Private _collection As BoxCollection
    Private curIndex As Integer
    Private curBox As Box


    Public Sub New(ByVal collection As BoxCollection)
        MyBase.New()
        _collection = collection
        curIndex = -1
        curBox = Nothing

    End Sub

    Private Property Box As Box
    Public Function MoveNext() As Boolean _
        Implements IEnumerator(Of Box).MoveNext
        curIndex = curIndex + 1
        If curIndex = _collection.Count Then
            ' Avoids going beyond the end of the collection.
            Return False
        Else
            'Set current box to next item in collection.
            curBox = _collection(curIndex)
        End If
        Return True
    End Function

    Public Sub Reset() _
        Implements IEnumerator(Of Box).Reset
        curIndex = -1
    End Sub

    Public Sub Dispose() _
        Implements IEnumerator(Of Box).Dispose

    End Sub

    Public ReadOnly Property Current() As Box _
        Implements IEnumerator(Of Box).Current

        Get
            If curBox Is Nothing Then
                Throw New InvalidOperationException()
            End If

            Return curBox
        End Get
    End Property

    Private ReadOnly Property Current1() As Object _
        Implements IEnumerator.Current

        Get
            Return Me.Current
        End Get
    End Property
End Class

' Defines two boxes as equal if they have the same dimensions.
Public Class BoxSameDimensions
    Inherits EqualityComparer(Of Box)

    Public Overrides Function Equals(ByVal b1 As Box, ByVal b2 As Box) As Boolean
        If b1.Height = b2.Height And b1.Length = b2.Length And b1.Width = b2.Width Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Overrides Function GetHashCode(ByVal bx As Box) As Integer
        Dim hCode As Integer = bx.Height ^ bx.Length ^ bx.Width
        Return hCode.GetHashCode()
    End Function
End Class

注釈

IEnumerator<T> は、すべてのジェネリック列挙子の基本インターフェイスです。

C# 言語の foreach ステートメント (C++ では for each、Visual Basic では For Each) では、列挙子の複雑さが隠されています。 したがって、列挙子を直接操作するのではなく、foreach を使用することをお勧めします。

列挙子を使用すると、コレクション内のデータを読み取ることができますが、基になるコレクションを変更することはできません。

最初、列挙子はコレクションの先頭の要素の前に位置付けられます。 この位置では、Current が未定義です。 そのため、MoveNext の値を読み取る前に、Current を呼び出して列挙子をコレクションの最初の要素に進める必要があります。

Current は、MoveNext が呼び出されるまでは同じオブジェクトを返します。 MoveNext は、Current を次の要素に進めます。

MoveNext がコレクションの末尾を通過した場合、列挙子がコレクション内の最後の要素の後に配置され、MoveNextfalse を返します。 列挙子がこの位置にある場合、後続の MoveNext 呼び出しも false を返します。 が返された falseCurrent の最後のMoveNext呼び出しが未定義の場合。 Current を、再度、コレクションの最初の要素に設定することはできません。列挙子の新しいインスタンスを作成する必要があります。

メソッドは Reset 、COM の相互運用性のために提供されます。 必ずしも実装する必要はありません。代わりに、実装者は 単に を NotSupportedExceptionスローできます。 ただし、これを行う場合は、呼び出し元が機能に依存していないことを確認する Reset 必要があります。

要素の追加、変更、削除など、コレクションに変更が加えられた場合、列挙子の動作は未定義です。

列挙子はコレクションに排他アクセスできないため、コレクションの列挙処理は本質的にスレッド セーフな処理ではありません。 列挙処理でスレッド セーフを確保するには、列挙処理が終わるまでコレクションをロックできます。 コレクションに対し複数のスレッドがアクセスして読み取りや書き込みを行うことができるようにするには、独自に同期化を実装する必要があります。

System.Collections.Generic 名前空間のコレクションの既定の実装は同期されません。

注意 (実装者)

このインターフェイスを実装するには、非ジェネリック IEnumerator インターフェイスを実装する必要があります。 メソッドと Reset() メソッドは MoveNext()T依存せず、非ジェネリック インターフェイスにのみ表示されます。 プロパティは Current 両方のインターフェイスに表示され、戻り値の型が異なります。 非ジェネリック Current プロパティを明示的なインターフェイス実装として実装します。 これにより、非ジェネリック インターフェイスのすべてのコンシューマーがジェネリック インターフェイスを使用できるようになります。

さらに、 IEnumerator<T> は を実装します IDisposable。これには、 メソッドを実装 Dispose() する必要があります。 これにより、他のリソースを使用するときに、データベース接続を閉じるか、ファイル ハンドルまたは同様の操作を解放できます。 破棄する追加のリソースがない場合は、空 Dispose() の実装を指定します。

プロパティ

Current

列挙子の現在位置にあるコレクション内の要素を取得します。

メソッド

Dispose()

アンマネージ リソースの解放またはリセットに関連付けられているアプリケーション定義のタスクを実行します。

(継承元 IDisposable)
MoveNext()

列挙子をコレクションの次の要素に進めます。

(継承元 IEnumerator)
Reset()

列挙子を初期位置、つまりコレクションの最初の要素の前に設定します。

(継承元 IEnumerator)

適用対象

こちらもご覧ください