语言

SortedDictionary<TKey,TValue>.Enumerator 结构

定义

枚举 . 的 SortedDictionary<TKey,TValue>元素

public: value class SortedDictionary<TKey, TValue>::Enumerator : System::Collections::Generic::IEnumerator<System::Collections::Generic::KeyValuePair<TKey, TValue>>, System::Collections::IDictionaryEnumerator
public struct SortedDictionary<TKey,TValue>.Enumerator : System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<TKey,TValue>>, System.Collections.IDictionaryEnumerator
type SortedDictionary<'Key, 'Value>.Enumerator = struct
    interface IEnumerator<KeyValuePair<'Key, 'Value>>
    interface IEnumerator
    interface IDisposable
    interface IDictionaryEnumerator
type SortedDictionary<'Key, 'Value>.Enumerator = struct
    interface IEnumerator<KeyValuePair<'Key, 'Value>>
    interface IDisposable
    interface IDictionaryEnumerator
    interface IEnumerator
type SortedDictionary<'Key, 'Value>.Enumerator = struct
    interface IEnumerator<KeyValuePair<'Key, 'Value>>
    interface IDisposable
    interface IEnumerator
    interface IDictionaryEnumerator
type SortedDictionary<'Key, 'Value>.Enumerator = struct
    interface IEnumerator<KeyValuePair<'Key, 'Value>>
    interface IDictionaryEnumerator
    interface IEnumerator
    interface IDisposable
Public Structure SortedDictionary(Of TKey, TValue).Enumerator
Implements IDictionaryEnumerator, IEnumerator(Of KeyValuePair(Of TKey, TValue))

类型参数

TKey
TValue
继承
SortedDictionary<TKey,TValue>.Enumerator
实现

注解

foreach C# 语言(For Each在 Visual Basic 中)的语句隐藏枚举器的复杂性。 因此,建议使用 foreach ,而不是直接操作枚举器。

枚举器可用于读取集合中的数据,但不能用于修改基础集合。

最初,枚举器位于集合中的第一个元素之前。 在此位置, Current 未定义。 在读取值MoveNext之前,必须调用Current该方法以将枚举器提升到集合的第一个元素。

该 Current 属性返回相同的对象,直到 MoveNext 调用为止。 MoveNext 设置为 Current 下一个元素。

如果 MoveNext 传递集合的末尾,则枚举器位于集合中的最后一个元素之后并 MoveNext 返回 false。 当枚举器处于此位置时,后续调用 MoveNext 也会返回 false。 如果返回MoveNext的最后一个调用false,Current则为未定义。 不能再次设置为 Current 集合的第一个元素;必须改为创建新的枚举器实例。

只要集合保持不变,枚举器就保持有效。 如果对集合进行了更改(例如添加、修改或删除元素),枚举器将不可恢复地失效,并且下次调用 MoveNext 或 IEnumerator.Reset 时会引发 InvalidOperationException 异常。

枚举器不具有对集合的独占访问权限;因此,通过集合进行枚举本质上不是线程安全的过程。 若要保证枚举期间的线程安全性,可以在整个枚举期间锁定集合。 若要允许多个线程访问集合进行读取和写入,必须实现自己的同步。

命名空间中 System.Collections.Generic 集合的默认实现不会同步。

属性

名称 说明
Current

获取枚举器当前位置的元素。

方法

名称 说明
Dispose()

释放该 SortedDictionary<TKey,TValue>.Enumerator命令使用的所有资源。

MoveNext()

将枚举器前进到下一个 SortedDictionary<TKey,TValue>元素。

显式接口实现

名称 说明
IDictionaryEnumerator.Entry

获取枚举器 DictionaryEntry 当前位置作为结构的元素。

IDictionaryEnumerator.Key

获取枚举器当前位置的元素的键。

IDictionaryEnumerator.Value

获取枚举器当前位置的元素的值。

IEnumerator.Current

获取枚举器当前位置的元素。

IEnumerator.Reset()

将枚举器设置为其初始位置,该位置位于集合中的第一个元素之前。

适用于

另请参阅