
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# ArrayList - TrimToSize() Method
The C# ArrayList TrimToSize() method is used to set the capacity to the actual number of elements in the ArrayList.
This method can reduce the memory overhead of a collection if no new elements are added to it.
This method takes O(n) operation, where n is the number of elements.
Syntax
Following is the syntax of the C# ArrayList TrimToSize() method −
public virtual void TrimToSize();
Parameters
This method does not accepts any parameters.
Return value
This method does not return any value.
Example 1: Demonstrate the Use of TrimToSize() Method
Following is the basic example of the TrimToSize() method to demonstrate how to use this method −
using System; using System.Collections; class Example { // Main method public static void Main() { // create and initialize new ArrayList ArrayList mylist = new ArrayList { "Hi", "tutorialspoint", "India", "Learn", "programming", "here" }; // Size of ArrayList before trimming Console.WriteLine("Before trimming the capacity is: {0}", mylist.Capacity); mylist.TrimToSize(); // Size of ArrayList after trimming Console.WriteLine("After trimming the capacity is: {0}", mylist.Capacity); } }
Output
Following is the output −
Before trimming the capacity is: 8 After trimming the capacity is: 6
Example 2: Trimming the ArrayList Size
Here, in this example, we create an ArrayList with a specified size. We then use the TrimToSize() method to set the capacity to the actual number of elements in the ArrayList −
using System; using System.Collections; class Program { static void Main() { // Creating an ArrayList with an initial capacity ArrayList myList = new ArrayList(10) { 1, 2, 3, 4, 5 }; Console.WriteLine($"Capacity before TrimToSize: {myList.Capacity}"); Console.WriteLine($"Count: {myList.Count}"); // Trimming the ArrayList to fit the actual number of elements myList.TrimToSize(); Console.WriteLine($"Capacity after TrimToSize: {myList.Capacity}"); } }
Output
Following is the output −
Capacity before TrimToSize: 10 Count: 5 Capacity after TrimToSize: 5
Example 3: Trimming After Removing Elements
This example shows how the TrimToSize() method is useful after removing elements from an ArrayList −
using System; using System.Collections; class Example { static void Main() { ArrayList list = new ArrayList() { 10, 20, 30, 40, 50, 60, 70, 80 }; Console.WriteLine($"Capacity before removing elements: {list.Capacity}"); Console.WriteLine($"Count: {list.Count}"); // Removing elements list.RemoveRange(2, 4); Console.WriteLine($"Capacity before TrimToSize: {list.Capacity}"); Console.WriteLine($"Count after removal: {list.Count}"); // Trimming the list to fit the remaining elements list.TrimToSize(); Console.WriteLine($"Capacity after TrimToSize: {list.Capacity}"); } }
Output
Following is the output −
Capacity before removing elements: 8 Count: 8 Capacity before TrimToSize: 8 Count after removal: 4 Capacity after TrimToSize: 4
Example 4: Dynamic TrimToSize Usage
This example shows adding multiple elements dynamically, removing some, and then trimming the ArrayList to free unused memory −
using System; using System.Collections; class Example { static void Main() { ArrayList numbers = new ArrayList(); for (int i = 1; i <= 8; i++) { numbers.Add(i); } Console.WriteLine($"Capacity before removing elements: {numbers.Capacity}"); Console.WriteLine($"Count before removing elements: {numbers.Count}"); // Remove some elements numbers.RemoveRange(5, 2); Console.WriteLine($"Capacity before TrimToSize: {numbers.Capacity}"); Console.WriteLine($"Count after removal: {numbers.Count}"); // Trim the ArrayList to free unused memory numbers.TrimToSize(); Console.WriteLine($"Capacity after TrimToSize: {numbers.Capacity}"); } }
Output
Following is the output −
Capacity before removing elements: 8 Count before removing elements: 8 Capacity before TrimToSize: 8 Count after removal: 6 Capacity after TrimToSize: 6