次の方法で共有


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 は未定義です。 したがって、Currentの値を読み取る前に、MoveNext を呼び出して列挙子をコレクションの最初の要素に進める必要があります。

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

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

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

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

列挙子は、コレクションへの排他的アクセス権を持っていません。したがって、コレクションを通じた列挙は、本質的にスレッド セーフなプロシージャではありません。 列挙中のスレッド セーフを保証するために、列挙体全体の間にコレクションをロックできます。 読み取りと書き込みのためにコレクションに複数のスレッドからアクセスできるようにするには、独自の同期を実装する必要があります。

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

注意 (実装者)

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

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

プロパティ

Current

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

メソッド

Dispose()

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

(継承元 IDisposable)
MoveNext()

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

(継承元 IEnumerator)
Reset()

列挙子を最初の位置 (コレクション内の最初の要素の前) に設定します。

(継承元 IEnumerator)

適用対象

こちらもご覧ください