HttpListenerPrefixCollection.GetEnumerator Metoda

Definicja

Zwraca obiekt, którego można użyć do iterowania w kolekcji.

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

Zwraca

IEnumerator<String>

Obiekt, który implementuje IEnumerator interfejs i zapewnia dostęp do ciągów w tej kolekcji.

Implementuje

Przykłady

W poniższym przykładzie kodu pokazano wyliczanie za pomocą kolekcji. Należy pamiętać, że przykłady Visual Basic i C# używają instrukcji specyficznych dla języka do wyliczania za pośrednictwem kolekcji zamiast pobierania modułu wyliczającego.

public static void DisplayPrefixesAndState(HttpListener listener)
{
    // List the prefixes to which the server listens.
    HttpListenerPrefixCollection prefixes = listener.Prefixes;
    if (prefixes.Count == 0)
    {
        Console.WriteLine("There are no prefixes.");
    }
    foreach(string prefix in prefixes)
    {
        Console.WriteLine(prefix);
    }
    // Show the listening state.
    if (listener.IsListening)
    {
        Console.WriteLine("The server is listening.");
    }
}
Public Shared Sub DisplayPrefixesAndState(ByVal listener As HttpListener)
    ' List the prefixes to which the server listens.
    Dim prefixes As HttpListenerPrefixCollection = listener.Prefixes

    If prefixes.Count = 0 Then
        Console.WriteLine("There are no prefixes.")
    End If

    For Each prefix As String In prefixes
        Console.WriteLine(prefix)
    Next

    ' Show the listening state.
    If listener.IsListening Then
        Console.WriteLine("The server is listening.")
    End If
End Sub

Uwagi

Obiekt zwracany przez tę metodę jest początkowo umieszczony przed pierwszym elementem w tej kolekcji. Przed uzyskaniem dostępu do pierwszego elementu należy wywołać metodę MoveNext . Aby uzyskać dostęp do elementu w bieżącej pozycji, wywołaj Current właściwość .

Nie należy modyfikować kolekcji podczas korzystania z modułu wyliczającego. Jeśli kolekcja jest modyfikowana podczas używania modułu wyliczającego, próba ustawienia pozycji przez wywołanie lub Reset spowoduje wywołanie MoveNext elementu InvalidOperationException.

Aby uzyskać szczegółowy opis modułów wyliczania, zobacz dokumentację IEnumerator dla klasy i GetEnumerator metody.

Dotyczy

Zobacz też