Export (0) Print
Expand All

ReadOnlyCollectionBase.GetEnumerator Method

Returns an enumerator that iterates through the ReadOnlyCollectionBase instance.

Namespace:  System.Collections
Assembly:  mscorlib (in mscorlib.dll)

Public Overridable Function GetEnumerator As IEnumerator



The foreach statement of the C# language (for each in Visual Basic) hides the complexity of the enumerators. Therefore, using foreach is recommended, instead of directly manipulating the enumerator.

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 in the collection. Reset also brings the enumerator back to this position. At this position, Current is undefined. 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.

If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false. If the last call to MoveNext returned false, Current is undefined. 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 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.

This method is an O(1) operation.

The following code example implements the ReadOnlyCollectionBase class.

Imports System
Imports System.Collections

Public Class ROCollection
    Inherits ReadOnlyCollectionBase

    Public Sub New(sourceList As IList)
    End Sub 'New 

    Default Public ReadOnly Property Item(index As Integer) As [Object]
            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()

        ' Create a new ROCollection that contains the elements in myAL. 
        Dim myCol As New ROCollection(myAL)

        ' Display the contents of the collection using For Each. This is the preferred method.
        Console.WriteLine("Contents of the collection (using For Each):")

        ' Display the contents of the collection using the enumerator.
        Console.WriteLine("Contents of the collection (using enumerator):")

        ' Display the contents of the collection using the Count property and the Item property.
        Console.WriteLine("Contents of the collection (using Count and Item):")

        ' 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"))

    End Sub 'Main

    ' Uses the Count property and the Item property. 
    Public Shared Sub PrintIndexAndValues(myCol As ROCollection)
        Dim i As Integer 
        For i = 0 To myCol.Count - 1
            Console.WriteLine("   [{0}]:   {1}", i, myCol(i))
        Next i
    End Sub 'PrintIndexAndValues

    ' Uses the For Each statement which hides the complexity of the enumerator. 
    ' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection. 
    Public Shared Sub PrintValues1(myCol As ROCollection)
        Dim obj As [Object]
        For Each obj In  myCol
            Console.WriteLine("   {0}", obj)
        Next obj
    End Sub 'PrintValues1

    ' Uses the enumerator.  
    ' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection. 
    Public Shared Sub PrintValues2(myCol As ROCollection)
        Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.WriteLine("   {0}", myEnumerator.Current)
        End While
    End Sub 'PrintValues2

End Class 'SamplesCollectionBase 

'This code produces the following output. 

'Contents of the collection (using For Each): 
'   red 
'   blue 
'   yellow 
'   green 
'   orange 
'   purple 

'Contents of the collection (using enumerator): 
'   red 
'   blue 
'   yellow 
'   green 
'   orange 
'   purple 

'Contents of the collection (using Count and Item): 
'   [0]:   red 
'   [1]:   blue 
'   [2]:   yellow 
'   [3]:   green 
'   [4]:   orange 
'   [5]:   purple 

'Contains yellow: True 
'orange is at index 4.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
© 2015 Microsoft