C# | List.TrimExcess Method - GeeksforGeeks

01 Feb, 2019
List<T>.TrimExcess Method is used to set the capacity to the actual number of elements in the List<T>, if that number is less than a threshold value.
Syntax:
public void TrimExcess ();
Note:
This method can be used to minimize a collection’s memory overhead if no new elements will be added to the collection.
To reset a List<T> to its initial state, call the Clear method before calling TrimExcess method.
Trimming an empty List<T> sets the capacity of the List<T> to the default capacity.
The cost of reallocating and copying a large List<T> can be considerable, however, so the TrimExcess 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.
This method is an O(n) operation, where n is Count.
Below programs illustrate the use of List<T>.TrimExcess Method:
Example 1:
// C# code to set the capacity to the// actual number of elements in the Listusing System;using System.Collections.Generic;  class GFG {      // Driver code    public static void Main()    {          // Creating a List of strings        List<string> mylist = new List<string>();          // Inserting elements into List        mylist.Add("1");        mylist.Add("2");        mylist.Add("3");        mylist.Add("4");        mylist.Add("5");          // To  display the capacity of list        Console.WriteLine(mylist.Capacity);          // To display number of elements in List        Console.WriteLine(mylist.Count);          // using TrimExcess method        mylist.TrimExcess();          // To  display the capacity of list        Console.WriteLine(mylist.Capacity);          // To display number of elements in List        Console.WriteLine(mylist.Count);    }}
Output:
8
5
5
5

Example 2:
// C# code to set the capacity to the// actual number of elements in the Listusing System;using System.Collections.Generic;  class GFG {      // Driver code    public static void Main()    {          // Creating a List of integers        List<int> mylist = new List<int>();          // Inserting elements into List        mylist.Add(45);        mylist.Add(78);        mylist.Add(32);        mylist.Add(231);        mylist.Add(123);        mylist.Add(76);        mylist.Add(726);        mylist.Add(716);        mylist.Add(876);          // To  display the capacity of list        Console.WriteLine(mylist.Capacity);          // To display number of elements in List        Console.WriteLine(mylist.Count);          // using TrimExcess method        mylist.TrimExcess();          // To  display the capacity of list        Console.WriteLine(mylist.Capacity);          // To display number of elements in List        Console.WriteLine(mylist.Count);          // using clear method        mylist.Clear();          // To  display the capacity of list        Console.WriteLine(mylist.Capacity);          // To display number of elements in List        Console.WriteLine(mylist.Count);    }}
Output:
16
9
9
9
9
0

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.trimexcess?view=netframework-4.7.2
CSharp-Generic-List
CSharp-Generic-Namespace
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
C# | Inheritance
Partial Classes in C#
C# | Generics - Introduction
Top 50 C# Interview Questions & Answers
Switch Statement in C#
C# | How to insert an element in an Array?
Convert String to Character Array in C#
Lambda Expressions in C#