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
type SortedDictionary<'Key, 'Value>.Enumerator = struct
    interface IEnumerator<KeyValuePair<'Key, 'Value>>
    interface IDictionaryEnumerator
    interface IDisposable
    interface IEnumerator
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 中的 C# 语言 (for each语句,Visual Basic) 隐藏枚举器的复杂性。 因此,建议使用 foreach,而不是直接操作枚举数。

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

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

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

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

只要集合保持不变,枚举器就仍有效。 如果对集合进行了更改,例如添加、修改或删除元素,枚举器将不可恢复地失效,并且下次调用 MoveNextIEnumerator.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()

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

适用于

另请参阅