C# ArrayList TrimToSize

Description

ArrayList TrimToSize sets the capacity to the actual number of elements in the ArrayList.

Syntax

ArrayList.TrimToSize has the following syntax.


public virtual void TrimToSize()

Returns

ArrayList.TrimToSize method returns

Example

The following code uses the TrimToSize() method to reduce the capacity of ArrayList.


using System;//w w w.ja v a 2s  .  c om
using System.Collections;

class MainClass
{
  public static void Main()
  {
    ArrayList myArrayList = new ArrayList();
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    

    string[] anotherStringArray = {"Here's", "some", "more", "text"};
    myArrayList.SetRange(0, anotherStringArray);

    myArrayList.TrimToSize();
    Console.WriteLine("myArrayList.Capacity = " + myArrayList.Capacity);

  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack