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

정의

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 ()

구현

예제

다음 예제를 만들고 채웁니다는 HashSet<T> 다음 삭제 하는 예제 컬렉션과 컬렉션에 의해 참조 되는 메모리를 해제 합니다.The following example creates and populates a HashSet<T> collection, then clears it and releases the memory referenced by the collection.

static void Main()
{
    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);

}
/* 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: { }

설명

Count 0으로 설정 된 한 컬렉션의 요소에서 다른 개체에 대 한 참조도 해제 됩니다.Count is set to zero and references to other objects from elements of the collection are also released. 용량에 대 한 호출 될 때까지 변경 되지 않습니다 TrimExcess 이루어집니다.The capacity remains unchanged until a call to TrimExcess is made.

이 메서드는 O (n) 작업, 여기서 nCount합니다.This method is an O(n) operation, where n is Count.

적용 대상