HashSet<T>.Clear HashSet<T>.Clear HashSet<T>.Clear HashSet<T>.Clear Method

Définition

Supprime tous les éléments d'un objet HashSet<T>.Removes all elements from a HashSet<T> object.

public:
 virtual void Clear();
public void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Sub Clear ()

Implémente

Exemples

L’exemple suivant crée et remplit une HashSet<T> collection, puis l’efface et libère la mémoire référencée par la collection.The following example creates and populates a HashSet<T> collection, then clears it and releases the memory referenced by the collection.

HashSet<int> Numbers = new HashSet<int>();

for (int i = 0; i < 10; i++)
{
    Numbers.Add(i);
}

Console.Write("Numbers contains {0} elements: ", Numbers.Count);
DisplaySet(Numbers);

Numbers.Clear();
Numbers.TrimExcess();

Console.Write("Numbers contains {0} elements: ", Numbers.Count);
DisplaySet(Numbers);

void DisplaySet(HashSet<int> set)
{
    Console.Write("{");
    foreach (int i in set)
    {
        Console.Write(" {0}", i);
    }
    Console.WriteLine(" }");
}

/* This example produces output similar to the following:
* Numbers contains 10 elements: { 0 1 2 3 4 5 6 7 8 9 }
* Numbers contains 0 elements: { }
*/
Shared Sub Main()

    Dim Numbers As HashSet(Of Integer) = New HashSet(Of Integer)()

    For i As Integer = 0 To 9
        Numbers.Add(i)
    Next i

    Console.Write("Numbers contains {0} elements: ", Numbers.Count)
    DisplaySet(Numbers)

    Numbers.Clear()
    Numbers.TrimExcess()

    Console.Write("Numbers contains {0} elements: ", Numbers.Count)
    DisplaySet(Numbers)

End Sub
' This code example produces output similar to the following:
' Numbers contains 10 elements: { 0 1 2 3 4 5 6 7 8 9 }
' Numbers contains 0 elements: { }

Remarques

Counta la valeur zéro et des références à d’autres objets provenant d’éléments de la collection sont également libérées.Count is set to zero and references to other objects from elements of the collection are also released. La capacité reste inchangée jusqu’à ce qu' TrimExcess un appel à soit effectué.The capacity remains unchanged until a call to TrimExcess is made.

Cette méthode est une opération On(), où n est Count.This method is an O(n) operation, where n is Count.

S’applique à