Επεξεργασία

Κοινή χρήση μέσω


HttpListenerPrefixCollection.GetEnumerator Method

Definition

Returns an object that can be used to iterate through the collection.

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)

Returns

An object that implements the IEnumerator interface and provides access to the strings in this collection.

Implements

Examples

The following code example demonstrates enumerating through a collection. Note that the Visual Basic and C# examples use language specific statements to enumerate through the collection instead of retrieving the enumerator.

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

Remarks

The object that is returned by this method is initially positioned before the first element in this collection. You must call the MoveNext method before you can access the first element. To access the element at the current position, call the Current property.

Do not modify the collection while using the enumerator. If the collection is modified while an enumerator is in use, an attempt to set the position by calling MoveNext or Reset causes an InvalidOperationException.

For a detailed description of enumerators, see the documentation for the IEnumerator class and the GetEnumerator method.

Applies to

See also