ReadOnlyCollectionBase.GetEnumerator Method
Returns an enumerator that can iterate through the ReadOnlyCollectionBase instance.
[Visual Basic] Public Overridable Function GetEnumerator() As IEnumerator _ Implements IEnumerable.GetEnumerator [C#] public virtual IEnumerator GetEnumerator(); [C++] public: virtual IEnumerator* GetEnumerator(); [JScript] public function GetEnumerator() : IEnumerator;
Return Value
An IEnumerator for the ReadOnlyCollectionBase instance.
Implements
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. Reset also brings the enumerator back to this position. At this position, calling Current throws an exception. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current.
Current returns the same object until either MoveNext or Reset is called. MoveNext sets 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 MoveNext returns false. If the last call to MoveNext returned false, calling Current throws an exception. To set Current to the first element of the collection again, you can call Reset followed by 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 MoveNext or Reset throws an InvalidOperationException. If the collection is modified between MoveNext and Current, 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.
Example
[Visual Basic, C#, C++] The following code example implements the ReadOnlyCollectionBase class.
[Visual Basic] Imports System Imports System.Collections Public Class ROCollection Inherits ReadOnlyCollectionBase Public Sub New(sourceList As IList) InnerList.AddRange(sourceList) End Sub 'New Default Public ReadOnly Property Item(index As Integer) As [Object] Get Return InnerList(index) End Get End Property Public Function IndexOf(value As [Object]) As Integer Return InnerList.IndexOf(value) End Function 'IndexOf Public Function Contains(value As [Object]) As Boolean Return InnerList.Contains(value) End Function 'Contains End Class 'ROCollection Public Class SamplesCollectionBase Public Shared Sub Main() ' Create an ArrayList. Dim myAL As New ArrayList() myAL.Add("red") myAL.Add("blue") myAL.Add("yellow") myAL.Add("green") myAL.Add("orange") myAL.Add("purple") ' Create a new ROCollection that contains the elements in myAL. Dim myCol As New ROCollection(myAL) ' Display the contents of the collection using the enumerator. Console.WriteLine("Contents of the collection (using enumerator):") PrintIndexAndValues(myCol) ' Search the collection with Contains and IndexOf. Console.WriteLine("Contains yellow: {0}", myCol.Contains("yellow")) Console.WriteLine("orange is at index {0}.", myCol.IndexOf("orange")) Console.WriteLine() ' Display the contents of the collection using the Count property and the Item property. Console.WriteLine("Contents of the collection (using Count and Item):") PrintIndexAndValues2(myCol) End Sub 'Main Public Shared Sub PrintIndexAndValues(myCol As ROCollection) Dim i As Integer = 0 Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator() While myEnumerator.MoveNext() Console.WriteLine(" [{0}]: {1}", i, myEnumerator.Current) i += 1 End While Console.WriteLine() End Sub 'PrintIndexAndValues Public Shared Sub PrintIndexAndValues2(myCol As ROCollection) Dim i As Integer For i = 0 To myCol.Count - 1 Console.WriteLine(" [{0}]: {1}", i, myCol(i)) Next i Console.WriteLine() End Sub 'PrintIndexAndValues2 End Class 'SamplesCollectionBase 'This code produces the following output. ' 'Contents of the collection (using enumerator): ' [0]: red ' [1]: blue ' [2]: yellow ' [3]: green ' [4]: orange ' [5]: purple ' 'Contains yellow: True 'orange is at index 4. ' 'Contents of the collection (using Count and Item): ' [0]: red ' [1]: blue ' [2]: yellow ' [3]: green ' [4]: orange ' [5]: purple [C#] using System; using System.Collections; public class ROCollection : ReadOnlyCollectionBase { public ROCollection( IList sourceList ) { InnerList.AddRange( sourceList ); } public Object this[ int index ] { get { return( InnerList[index] ); } } public int IndexOf( Object value ) { return( InnerList.IndexOf( value ) ); } public bool Contains( Object value ) { return( InnerList.Contains( value ) ); } } public class SamplesCollectionBase { public static void Main() { // Create an ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "red" ); myAL.Add( "blue" ); myAL.Add( "yellow" ); myAL.Add( "green" ); myAL.Add( "orange" ); myAL.Add( "purple" ); // Create a new ROCollection that contains the elements in myAL. ROCollection myCol = new ROCollection( myAL ); // Display the contents of the collection using the enumerator. Console.WriteLine( "Contents of the collection (using enumerator):" ); PrintIndexAndValues( myCol ); // Search the collection with Contains and IndexOf. Console.WriteLine( "Contains yellow: {0}", myCol.Contains( "yellow" ) ); Console.WriteLine( "orange is at index {0}.", myCol.IndexOf( "orange" ) ); Console.WriteLine(); // Display the contents of the collection using the Count property and the Item property. Console.WriteLine( "Contents of the collection (using Count and Item):" ); PrintIndexAndValues2( myCol ); } public static void PrintIndexAndValues( ROCollection myCol ) { int i = 0; System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.WriteLine( " [{0}]: {1}", i++, myEnumerator.Current ); Console.WriteLine(); } public static void PrintIndexAndValues2( ROCollection myCol ) { for ( int i = 0; i < myCol.Count; i++ ) Console.WriteLine( " [{0}]: {1}", i, myCol[i] ); Console.WriteLine(); } } /* This code produces the following output. Contents of the collection (using enumerator): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple Contains yellow: True orange is at index 4. Contents of the collection (using Count and Item): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple */ [C++] #using <mscorlib.dll> using namespace System; using namespace System::Collections; public __gc class ROCollection : public ReadOnlyCollectionBase { public: ROCollection( IList* sourceList ) { InnerList->AddRange( sourceList ); } public: __property Object* get_Item( int index ) { return( InnerList->Item[index] ); } public: int IndexOf( Object* value ) { return( InnerList->IndexOf( value ) ); } public: bool Contains( Object* value ) { return( InnerList->Contains( value ) ); } }; static void PrintIndexAndValues( ROCollection* myCol ) { int i = 0; System::Collections::IEnumerator* myEnumerator = myCol->GetEnumerator(); while ( myEnumerator->MoveNext() ) Console::WriteLine( S" [{0}]: {1}", __box(i++), myEnumerator->Current ); Console::WriteLine(); } static void PrintIndexAndValues2( ROCollection* myCol ) { for ( int i = 0; i < myCol->Count; i++ ) Console::WriteLine( S" [{0}]: {1}", __box(i), myCol->Item[i] ); Console::WriteLine(); } int main() { // Create an ArrayList. ArrayList* myAL = new ArrayList(); myAL->Add( S"red" ); myAL->Add( S"blue" ); myAL->Add( S"yellow" ); myAL->Add( S"green" ); myAL->Add( S"orange" ); myAL->Add( S"purple" ); // Create a new ROCollection that contains the elements in myAL. ROCollection* myCol = new ROCollection( myAL ); // Display the contents of the collection using the enumerator. Console::WriteLine( S"Contents of the collection (using enumerator):" ); PrintIndexAndValues( myCol ); // Search the collection with Contains and IndexOf. Console::WriteLine( S"Contains yellow: {0}", __box(myCol->Contains( S"yellow" ))); Console::WriteLine( S"orange is at index {0}.", __box(myCol->IndexOf( S"orange" ))); Console::WriteLine(); // Display the contents of the collection using the Count property and the Item property. Console::WriteLine( S"Contents of the collection (using Count and Item):" ); PrintIndexAndValues2( myCol ); } /* This code produces the following output. Contents of the collection (using enumerator): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple Contains yellow: True orange is at index 4. Contents of the collection (using Count and Item): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
ReadOnlyCollectionBase Class | ReadOnlyCollectionBase Members | System.Collections Namespace | System.Collections.IEnumerator