Mono Class Library: System.Array Overview | MembersSystem.Array.GetEnumerator Method |
Returns a IEnumerator for the current instance. [Edit]
|
A IEnumerator for the current instance. [Edit]
A IEnumerator grants read-access to the elements of a Array.
Note: This method is implemented to support the IEnumerator interface. For more information regarding the use of an enumerator, see IEnumerator.Operation
Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
Initially, the enumerator is positioned before the first element of the current instance. IEnumerator.Reset returns the enumerator to this position. Therefore, after an enumerator is created or after a IEnumerator.Reset, IEnumerator.MoveNext is required to be called 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.
If IEnumerator.MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and IEnumerator.MoveNext returns false. When the enumerator is at this position, subsequent calls toIEnumerator.MoveNext also return false. If the last call to IEnumerator.MoveNext returned false, IEnumerator.Current is unspecified. 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 its behavior is undefined.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
Multidimensional arrays will be processed in Row-major form.
Note: For some multidimensional Array objects, it can be desirable for an enumerator to process them in Column-major form.Note to Inheritors
Override this method to provide read-access to the current instance.Usage
Use this method to iterate over the elements of the current instance. [Edit]
This example demonstrates the Array.GetEnumerator method.
C# Example using System; using System.Collections; public class ArrayGetEnumerator { public static void Main() { string[,] strAry = {{"1","one"}, {"2", "two"}, {"3", "three"}}; Console.Write( "The elements of the array are: " ); IEnumerator sEnum = strAry.GetEnumerator(); while ( sEnum.MoveNext() ) Console.Write( " {0}", sEnum.Current ); } }The output is
The elements of the array are: 1 one 2 two 3 three
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0