public interface IDictionaryEnumerator : IEnumerator
|
Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.
IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.
After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
Entry | Read-only When implemented by a class, gets both the key and the value of the current dictionary entry. |
Key | Read-only When implemented by a class, gets the key of the current dictionary entry. |
Value | Read-only When implemented by a class, gets the value of the current dictionary entry. |
Hierarchy:
DictionaryEntry Entry {get;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The IDictionaryEnumerator is positioned before the first entry of the dictionary or after the last entry. -or- The dictionary is modified after the IDictionaryEnumerator was created. |
IDictionaryEnumerator.Entry also throws an exception if the last call to IEnumerator.MoveNext returned false, which indicates the end of the collection.
IDictionaryEnumerator.Entry does not move the position of the enumerator and consecutive calls to IDictionaryEnumerator.Entry return the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called.
object Key {get;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The IDictionaryEnumerator is positioned before the first entry of the dictionary or after the last entry. -or- The dictionary is modified after the IDictionaryEnumerator was created. |
IDictionaryEnumerator.Key also throws an exception if the last call to IEnumerator.MoveNext returned false, which indicates the end of the collection.
IDictionaryEnumerator.Key does not move the position of the enumerator and consecutive calls to IDictionaryEnumerator.Key return the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called.
object Value {get;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | The IDictionaryEnumerator is positioned before the first entry of the dictionary or after the last entry. -or- The dictionary is modified after the IDictionaryEnumerator was created. |
IDictionaryEnumerator.Value also throws an exception if the last call to IEnumerator.MoveNext returned false, which indicates the end of the collection.
IDictionaryEnumerator.Value does not move the position of the enumerator and consecutive calls to IDictionaryEnumerator.Value return the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called.