Share via


IEnumerator.Current 属性

获取集合中的当前元素。

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

语法

声明
ReadOnly Property Current As Object
用法
Dim instance As IEnumerator
Dim value As Object

value = instance.Current
Object Current { get; }
property Object^ Current {
    Object^ get ();
}
/** @property */
Object get_Current ()
function get Current () : Object

属性值

集合中的当前元素。

异常

异常类型 条件

InvalidOperationException

枚举数定位在该集合的第一个元素之前或最后一个元素之后。

- 或 -

在创建了枚举数后集合被修改了。

备注

创建枚举数或调用 Reset 方法后,必须先通过调用 MoveNext 方法将枚举数前移到集合的第一个元素,然后才能读取 Current 属性的值;否则 Current 是未定义的。

如果对 MoveNext 的上一次调用返回 false(它指示集合的结尾),则 Current 也会引发异常。

Current 不移动枚举数的位置,而且在调用 MoveNextReset 之前,对 Current 的连续调用返回相同的对象。

只要集合保持不变,枚举数就保持有效。如果对集合进行了更改(如添加、修改或删除元素),则枚举数将失效且不可恢复,并且下一次对 MoveNextReset 的调用将引发 InvalidOperationException。如果在 MoveNextCurrent 之间修改集合,那么即使枚举数已经无效,Current 也将返回它所设置成的元素。

示例

下面的代码示例演示如何实现自定义集合的 IEnumerator 接口。在此示例中,没有显式调用但实现了 Current,以便支持使用 foreach(在 Visual Basic 中为 for each)。此代码示例摘自 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
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;
    }

    public object Current
    {
        get
        {
            try
            {
                return _people[position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }
}

平台

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

请参见

参考

IEnumerator 接口
IEnumerator 成员
System.Collections 命名空间
MoveNext
Reset