ReadOnlyCollection<T>.GetEnumerator Methode

Definitie

Retourneert een enumerator die door de ReadOnlyCollection<T>.

public:
 virtual System::Collections::Generic::IEnumerator<T> ^ GetEnumerator();
public System.Collections.Generic.IEnumerator<T> GetEnumerator();
abstract member GetEnumerator : unit -> System.Collections.Generic.IEnumerator<'T>
override this.GetEnumerator : unit -> System.Collections.Generic.IEnumerator<'T>
Public Function GetEnumerator () As IEnumerator(Of T)

Retouren

Een IEnumerator<T> voor de ReadOnlyCollection<T>.

Implementeringen

Voorbeelden

In het volgende codevoorbeeld wordt de enumerator gebruikt om de inhoud weer te geven van een die een ReadOnlyCollection<T>List<T>. De opsommingsfunctie wordt verborgen door de instructie foreach (For Each in Visual Basic).

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Tyrannosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Deinonychus");
        dinosaurs.Add("Compsognathus");

        ReadOnlyCollection<string> readOnlyDinosaurs =
            new ReadOnlyCollection<string>(dinosaurs);

        Console.WriteLine();
        foreach( string dinosaur in readOnlyDinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nCount: {0}", readOnlyDinosaurs.Count);

        Console.WriteLine("\nContains(\"Deinonychus\"): {0}",
            readOnlyDinosaurs.Contains("Deinonychus"));

        Console.WriteLine("\nreadOnlyDinosaurs[3]: {0}",
            readOnlyDinosaurs[3]);

        Console.WriteLine("\nIndexOf(\"Compsognathus\"): {0}",
            readOnlyDinosaurs.IndexOf("Compsognathus"));

        Console.WriteLine("\nInsert into the wrapped List:");
        Console.WriteLine("Insert(2, \"Oviraptor\")");
        dinosaurs.Insert(2, "Oviraptor");

        Console.WriteLine();
        foreach( string dinosaur in readOnlyDinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        string[] dinoArray = new string[readOnlyDinosaurs.Count + 2];
        readOnlyDinosaurs.CopyTo(dinoArray, 1);

        Console.WriteLine("\nCopied array has {0} elements:",
            dinoArray.Length);
        foreach( string dinosaur in dinoArray )
        {
            Console.WriteLine("\"{0}\"", dinosaur);
        }
    }
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Deinonychus
Compsognathus

Count: 4

Contains("Deinonychus"): True

readOnlyDinosaurs[3]: Compsognathus

IndexOf("Compsognathus"): 3

Insert into the wrapped List:
Insert(2, "Oviraptor")

Tyrannosaurus
Amargasaurus
Oviraptor
Deinonychus
Compsognathus

Copied array has 7 elements:
""
"Tyrannosaurus"
"Amargasaurus"
"Oviraptor"
"Deinonychus"
"Compsognathus"
""
 */
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of String)

        dinosaurs.Add("Tyrannosaurus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("Deinonychus")
        dinosaurs.Add("Compsognathus")

        Dim readOnlyDinosaurs As _
            New ReadOnlyCollection(Of String)(dinosaurs)

        Console.WriteLine()
        For Each dinosaur As String In readOnlyDinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "Count: {0}", _
            readOnlyDinosaurs.Count)

        Console.WriteLine(vbLf & "Contains(""Deinonychus""): {0}", _
            readOnlyDinosaurs.Contains("Deinonychus"))

        Console.WriteLine(vbLf & _
            "readOnlyDinosaurs(3): {0}", readOnlyDinosaurs(3))

        Console.WriteLine(vbLf & "IndexOf(""Compsognathus""): {0}", _
            readOnlyDinosaurs.IndexOf("Compsognathus"))

        Console.WriteLine(vbLf & "Insert into the wrapped List:")
        Console.WriteLine("Insert(2, ""Oviraptor"")")
        dinosaurs.Insert(2, "Oviraptor")

        Console.WriteLine()
        For Each dinosaur As String In readOnlyDinosaurs
            Console.WriteLine(dinosaur)
        Next

        Dim dinoArray(readOnlyDinosaurs.Count + 1) As String
        readOnlyDinosaurs.CopyTo(dinoArray, 1)

        Console.WriteLine(vbLf & "Copied array has {0} elements:", _
            dinoArray.Length)
        For Each dinosaur As String In dinoArray
            Console.WriteLine("""{0}""", dinosaur)
        Next

   End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Deinonychus
'Compsognathus
'
'Count: 4
'
'Contains("Deinonychus"): True
'
'readOnlyDinosaurs(3): Compsognathus
'
'IndexOf("Compsognathus"): 3
'
'Insert into the wrapped List:
'Insert(2, "Oviraptor")
'
'Tyrannosaurus
'Amargasaurus
'Oviraptor
'Deinonychus
'Compsognathus
'
'Copied array has 7 elements:
'""
'"Tyrannosaurus"
'"Amargasaurus"
'"Oviraptor"
'"Deinonychus"
'"Compsognathus"
'""

Opmerkingen

De instructie foreach van de C#-taal (For Each in Visual Basic) verbergt de complexiteit van de opsommingen. Daarom wordt het gebruik foreach aanbevolen in plaats van de opsomming rechtstreeks te bewerken.

Enumerators kunnen worden gebruikt om de gegevens in de verzameling te lezen, maar ze kunnen niet worden gebruikt om de onderliggende verzameling te wijzigen.

In eerste instantie wordt de enumerator vóór het eerste element in de verzameling weergegeven. Op deze positie Current is niet gedefinieerd. Daarom moet u de opsomming doorschakelen MoveNext naar het eerste element van de verzameling voordat u de waarde van Current.

Current retourneert hetzelfde object totdat MoveNext het wordt aangeroepen. MoveNext wordt ingesteld Current op het volgende element.

Als MoveNext het einde van de verzameling wordt doorgegeven, wordt de enumerator geplaatst na het laatste element in de verzameling en MoveNext wordt het resultaat geretourneerd false. Wanneer de enumerator zich op deze positie bevindt, worden volgende aanroepen ook MoveNext geretourneerd false. Als de laatste aanroep die moet MoveNext worden geretourneerd false, Current niet is gedefinieerd. U kunt niet opnieuw instellen Current op het eerste element van de verzameling. U moet in plaats daarvan een nieuw enumerator-exemplaar maken.

Een enumerator blijft geldig zolang de verzameling ongewijzigd blijft. Als er wijzigingen worden aangebracht in de verzameling, zoals het toevoegen, wijzigen of verwijderen van elementen, is de enumerator onherstelbaar ongeldig en is het gedrag ervan niet gedefinieerd.

De enumerator heeft geen exclusieve toegang tot de verzameling; Daarom is het inventariseren via een verzameling intrinsiek geen thread-veilige procedure. Om de veiligheid van threads tijdens de inventarisatie te garanderen, kunt u de verzameling vergrendelen tijdens de gehele inventarisatie. Als u wilt toestaan dat de verzameling toegankelijk is voor meerdere threads voor lezen en schrijven, moet u uw eigen synchronisatie implementeren.

Standaard implementaties van verzamelingen in System.Collections.Generic worden niet gesynchroniseerd.

Deze methode is een O(1)-bewerking.

Van toepassing op

Zie ook