Sets the capacity to the actual number of elements in the List(T), if that number is less than a threshold value.
Assembly: mscorlib (in mscorlib.dll)
This method can be used to minimize a collection's memory overhead if no new elements will be added to the collection. The cost of reallocating and copying a large List(T) can be considerable, however, so the method does nothing if the list is at more than 90 percent of capacity. This avoids incurring a large reallocation cost for a relatively small gain.
The current threshold of 90 percent might change in future releases.
This method is an O(n) operation, where n is Count.
The capacity can also be set using the Capacity property.
The following example demonstrates how to check the capacity and count of a List(T) that contains a simple business object, and illustrates using the method to remove extra capacity.
The following example demonstrates the method. Several properties and methods of the List(T) class are used to add, insert, and remove items from a list of strings. Then the method is used to reduce the capacity to match the count, and the Capacity and Count properties are displayed. If the unused capacity had been less than 10 percent of total capacity, the list would not have been resized. Finally, the contents of the list are cleared.
[<EntryPoint>] let main argv = // We refer to System.Collections.Generic.List<'T> by its type // abbreviation ResizeArray<'T> to avoid conflict with the List module. // Note: In F# code, F# linked lists are usually preferred over // ResizeArray<'T> when an extendable collection is required. let dinosaurs = ResizeArray<_>() // Write out the dinosaurs in the ResizeArray. let printDinosaurs() = printfn "" dinosaurs |> Seq.iter (fun p -> printfn "%O" p) printfn "\nCapacity: %i" dinosaurs.Capacity dinosaurs.Add("Tyrannosaurus") dinosaurs.Add("Amargasaurus") dinosaurs.Add("Mamenchisaurus") dinosaurs.Add("Deinonychus") dinosaurs.Add("Compsognathus") printDinosaurs() printfn "\nCapacity: %i" dinosaurs.Capacity printfn "Count: %i" dinosaurs.Count printfn "\nContains(\"Deinonychus\"): %b" (dinosaurs.Contains("Deinonychus")) printfn "\nInsert(2, \"Compsognathus\")" dinosaurs.Insert(2, "Compsognathus") printDinosaurs() // Shows accessing the list using the Item property. printfn "\ndinosaurs: %s" dinosaurs. printfn "\nRemove(\"Compsognathus\")" dinosaurs.Remove("Compsognathus") |> ignore printDinosaurs() dinosaurs.TrimExcess() printfn "\nTrimExcess()" printfn "Capacity: %i" dinosaurs.Capacity printfn "Count: %i" dinosaurs.Count dinosaurs.Clear() printfn "\nClear()" printfn "Capacity: %i" dinosaurs.Capacity printfn "Count: %i" dinosaurs.Count 0 // return an integer exit code (* This code example produces the following output: Capacity: 0 Tyrannosaurus Amargasaurus Mamenchisaurus Deinonychus Compsognathus Capacity: 8 Count: 5 Contains("Deinonychus"): true Insert(2, "Compsognathus") Tyrannosaurus Amargasaurus Compsognathus Mamenchisaurus Deinonychus Compsognathus dinosaurs: Mamenchisaurus Remove("Compsognathus") Tyrannosaurus Amargasaurus Mamenchisaurus Deinonychus Compsognathus TrimExcess() Capacity: 5 Count: 5 Clear() Capacity: 5 Count: 0 *)
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
XNA FrameworkSupported in: 3.0, 2.0, 1.0
Portable Class LibrarySupported in: Portable Class Library
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8