System.Collections.IDictionaryEnumerator Interface

Assembly: Mscorlib.dll
Namespace: System.Collections
Summary
Enumerates the elements of a dictionary.
C# Syntax:
public interface IDictionaryEnumerator : IEnumerator
Remarks
Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

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.

See also:
System.Collections Namespace | IDictionary | IEnumerator

System.Collections.IDictionaryEnumerator Member List:

Public Properties
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:


System.Collections.IDictionaryEnumerator Member Details

Property: Entry (read-only)
Summary
When implemented by a class, gets both the key and the value of the current dictionary entry.
C# Syntax:
DictionaryEntry Entry {get;}
Exceptions
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.

Remarks
After an enumerator is created or after a IEnumerator.Reset, IEnumerator.MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of IDictionaryEnumerator.Entry; otherwise, IDictionaryEnumerator.Entry is undefined.

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.

See also:
IEnumerator.Reset | IEnumerator.MoveNext | IDictionaryEnumerator.Key | IDictionaryEnumerator.Value

Return to top


Property: Key (read-only)
Summary
When implemented by a class, gets the key of the current dictionary entry.
C# Syntax:
object Key {get;}
Exceptions
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.

Remarks
After an enumerator is created or after a IEnumerator.Reset, IEnumerator.MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of IDictionaryEnumerator.Key; otherwise, IDictionaryEnumerator.Key is undefined.

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.

See also:
IEnumerator.Reset | IEnumerator.MoveNext | IDictionaryEnumerator.Value | IDictionaryEnumerator.Entry

Return to top


Property: Value (read-only)
Summary
When implemented by a class, gets the value of the current dictionary entry.
C# Syntax:
object Value {get;}
Exceptions
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.

Remarks
After an enumerator is created or after a IEnumerator.Reset, IEnumerator.MoveNext must be called to advance the enumerator to the first element of the collection before reading the value of IDictionaryEnumerator.Value; otherwise, IDictionaryEnumerator.Value is undefined.

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.

See also:
IEnumerator.Reset | IEnumerator.MoveNext | IDictionaryEnumerator.Key | IDictionaryEnumerator.Entry

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.