Edit

Share via


HashSet<T>.TrimExcess Method

Definition

Overloads

TrimExcess()

Sets the capacity of a HashSet<T> object to the actual number of elements it contains, rounded up to a nearby, implementation-specific value.

TrimExcess(Int32)

Sets the capacity of a HashSet<T> object to the specified number of entries, rounded up to a nearby, implementation-specific value.

TrimExcess()

Source:
HashSet.cs
Source:
HashSet.cs
Source:
HashSet.cs

Sets the capacity of a HashSet<T> object to the actual number of elements it contains, rounded up to a nearby, implementation-specific value.

public:
 void TrimExcess();
public void TrimExcess ();
member this.TrimExcess : unit -> unit
Public Sub TrimExcess ()

Examples

The following example creates and populates a HashSet<T> collection, and then clears the collection and releases the memory referenced by it.

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: { }
*/
let displaySet (set: HashSet<int>) =
    printf "{"

    for i in set do
        printf $" {i}"

    printfn " }"
// 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: { }
let numbers = HashSet<int>()

for i = 0 to 9 do
    numbers.Add i |> ignore

printf $"Numbers contains {numbers.Count} elements: "
displaySet numbers

numbers.Clear()
numbers.TrimExcess()

printf $"Numbers contains {numbers.Count} elements: "
displaySet numbers
Imports System.Collections.Generic

Class Program

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

    Private Shared Sub DisplaySet(ByVal coll As HashSet(Of Integer))
        Console.Write("{")
        For Each i As Integer In coll
            Console.Write(" {0}", i)
        Next i
        Console.WriteLine(" }")
    End Sub

End Class

Remarks

You can use the TrimExcess method to minimize a HashSet<T> object's memory overhead once it is known that no new elements will be added. To completely clear a HashSet<T> object and release all memory referenced by it, call this method after calling the Clear method.

This method is an O(n) operation, where n is Count.

Applies to

TrimExcess(Int32)

Source:
HashSet.cs

Sets the capacity of a HashSet<T> object to the specified number of entries, rounded up to a nearby, implementation-specific value.

public:
 void TrimExcess(int capacity);
public void TrimExcess (int capacity);
member this.TrimExcess : int -> unit
Public Sub TrimExcess (capacity As Integer)

Parameters

capacity
Int32

The new capacity.

Exceptions

The specified capacity is lower than the count of entries.

Applies to