IEnumerator.Current Eigenschaft
Definition
Ruft das Element in der Auflistung an der aktuellen Position des Enumerators ab.Gets the element in the collection at the current position of the enumerator.
public:
property System::Object ^ Current { System::Object ^ get(); };
public object Current { get; }
member this.Current : obj
Public ReadOnly Property Current As Object
Eigenschaftswert
Das Element in der Auflistung an der aktuellen Position des Enumerators.The element in the collection at the current position of the enumerator.
Beispiele
Im folgenden Codebeispiel wird die Implementierung der- IEnumerator Schnittstellen für eine benutzerdefinierte Auflistung veranschaulicht.The following code example demonstrates the implementation of the IEnumerator interfaces for a custom collection. In diesem Beispiel Current wird nicht explizit aufgerufen, aber implementiert, um die Verwendung von foreach
( for each
in Visual Basic) zu unterstützen.In this example, Current is not explicitly called, but it is implemented to support the use of foreach
(for each
in Visual Basic). Dieses Codebeispiel ist Teil eines größeren Beispiels für die- IEnumerator Schnittstelle.This code example is part of a larger example for the IEnumerator interface.
// When you implement IEnumerable, you must also implement IEnumerator.
public class PeopleEnum : IEnumerator
{
public Person[] _people;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
' When you implement IEnumerable, you must also implement IEnumerator.
Public Class PeopleEnum
Implements IEnumerator
Public _people() As Person
' Enumerators are positioned before the first element
' until the first MoveNext() call.
Dim position As Integer = -1
Public Sub New(ByVal list() As Person)
_people = list
End Sub
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
position = position + 1
Return (position < _people.Length)
End Function
Public Sub Reset() Implements IEnumerator.Reset
position = -1
End Sub
Public ReadOnly Property Current() As Object Implements IEnumerator.Current
Get
Try
Return _people(position)
Catch ex As IndexOutOfRangeException
Throw New InvalidOperationException()
End Try
End Get
End Property
End Class
Hinweise
Current ist unter den folgenden Bedingungen nicht definiert:Current is undefined under any of the following conditions:
Der Enumerator ist vor dem Erstellen des Enumerators vor dem ersten Element in der Auflistung positioniert.The enumerator is positioned before the first element in the collection, immediately after the enumerator is created. MoveNext muss aufgerufen werden, um den Enumerator vor dem Lesen des Werts von auf das erste Element der Auflistung zu setzen Current .MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of Current.
Der letzte MoveNext zurückgegebene Rückruf
false
, der das Ende der Auflistung angibt.The last call to MoveNext returnedfalse
, which indicates the end of the collection.Der Enumerator wird aufgrund der in der Auflistung vorgenommenen Änderungen ungültig, z. b. durch Hinzufügen, ändern oder Löschen von Elementen.The enumerator is invalidated due to changes made in the collection, such as adding, modifying, or deleting elements.
Current gibt solange dasselbe Objekt zurück, bis MoveNext aufgerufen wird.Current returns the same object until MoveNext is called. MoveNext legt Current auf das nächste Element fest.MoveNext sets Current to the next element.