C# | Setting the capacity to the actual number of elements in a SortedList object

SortedList.TrimToSize Method is used to set the capacity to the actual number of elements in a SortedList object.

Syntax:

public virtual void TrimToSize ();

Exception: This method will throw NotSupportedException if the SortedList object is read-only or SortedList has a fixed size.



Below programs illustrate the use of above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// SortedList.TrimToSize() Method
using System;
using System.Collections;
  
class GFG {
  
    // Main method
    public static void Main()
    {
  
        // create and initalize new SortedList
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("1", "C++");
        mylist.Add("2", "Java");
        mylist.Add("3", "DSA");
        mylist.Add("4", "Python");
        mylist.Add("5", "C#");
        mylist.Add("6", "HTML");
  
        // Capacity of SortedList before trimmimg
        Console.WriteLine("Before trimming the capacity is: {0}",
                                                mylist.Capacity);
  
        // trim the SortedList
        mylist.TrimToSize();
  
        // Capacity of ArrayList after trimming
        Console.WriteLine("After trimming the capacity is: {0}",
                                               mylist.Capacity);
    }
}

chevron_right


Output:

Before trimming the capacity is: 16
After trimming the capacity is: 6

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate
// SortedList.TrimToSize() Method
using System;
using System.Collections;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // create and initalize new SortedList
        SortedList mylist = new SortedList();
  
        // Adding elements to SortedList
        mylist.Add("h", "Hello"); 
        mylist.Add("g", "Geeks!"); 
        mylist.Add("w", "Welcome");
        mylist.Add("t", "to");
        mylist.Add("n", "Noida"); 
  
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine("Before Using TrimToSize Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
  
        Console.WriteLine();
  
        // Trim the SortedList.
        mylist.TrimToSize();
  
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine();
        Console.WriteLine("After Using TrimToSize Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
  
        // Clear the SortedList
        mylist.Clear();
  
        Console.WriteLine();
  
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine();
        Console.WriteLine("After Using Clear Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
  
        // again trim the SortedList
        mylist.TrimToSize();
  
        Console.WriteLine();
  
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine();
        Console.WriteLine("After Again Using TrimToSize Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
    }
  
    // to display the values of SortedList
    public static void Display(SortedList mylist)
    {
        // taking an IList and
        // using GetValueList method
        IList vlist = mylist.GetValueList();
  
        for (int i = 0; i < mylist.Count; i++)
            Console.Write(vlist[i] + " ");
    }
}

chevron_right


Output:

Before Using TrimToSize Method:

Count: 5
Capacity: 16
Values are: Geeks! Hello Noida to Welcome 

After Using TrimToSize Method:

Count: 5
Capacity: 5
Values are: Geeks! Hello Noida to Welcome 

After Using Clear Method:

Count: 0
Capacity: 5
Values are: 

After Again Using TrimToSize Method:

Count: 0
Capacity: 0
Values are:

Note:

  • The main use of this method is that it can be used to minimize a collection’s memory overhead if no new elements will be added to the collection.
  • To reset a SortedList object to its initial state, call the Clear method before calling TrimToSize. Trimming an empty SortedList set the capacity of the SortedList to the default capacity.
  • This method is an O(n) operation, where n is Count.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.