C# Array.Resize Changes Array Lengths

Conversion or change

You have an array in your C# program and want to make it bigger or smaller, holding more or fewer elements. This uses memory efficiently, but can be slow to execute. Here we use the Array.Resize method, showing how it reallocates and copies the elements in the array to a new one. This can hurt performance, but can also help it.

Resize smaller

This first example shows how you can use Array.Resize to shrink an array. This is useful if you have a large array of data, and want to only keep the first N bytes or elements. The Array.Resize call here changes an array with four elements to one with two elements.

This C# example program uses the Array.Resize method. It grows and shrinks an array.

Program that uses Array.Resize [C#]

using System;

class Program
{
    static void Main()
    {
	// Initialize array for example
	char[] array = new char[4];
	array[0] = 'p';
	array[1] = 'e';
	array[2] = 'r';
	array[3] = 'l';

	// Display the array
	for (int i = 0; i < array.Length; i++)
	{
	    Console.Write(array[i]);
	}
	Console.WriteLine();

	// Resize the array from 4 to 2 element
	Array.Resize(ref array, 2);

	// Display the array that has been resized.
	for (int i = 0; i < array.Length; i++)
	{
	    Console.Write(array[i]);
	}
	Console.WriteLine();
    }
}

Output
    (Array was resized to 2 elements.)

perl
pe

Note. Internally, the single call to Array.Resize here runs through an algorithm that determines that the array needs to be smaller. So it copies the referenced array to a new array, and then changes the reference.

Resize bigger

Here we look at an example of how you can expand an array with Array.Resize. This can be useful for certain data structures, such as those that must accommodate more data but have minimal memory footprint. I have used this in implementing hash table buckets.

Another program that uses Array.Resize [C#]

using System;

class Program
{
    static void Main()
    {
	// Initialize an array with 5 elements.
	char[] arr = new char[5];
	arr[0] = 'p';
	arr[1] = 'y';
	arr[2] = 't';
	arr[3] = 'h';
	arr[4] = 'o';

	// We need an array with 6 elements!
	// Use Array.Resize to make it bigger.
	Array.Resize<char>(ref arr, 6);

	// Assign the last element.
	arr[5] = 'n';

	// Display the array
	Console.WriteLine(new string(arr));
    }
}

Output
    (Has 6 elements now.)

python

Description. This example shows how you can make a char array bigger and then assign the new elements to chars. If you omit the Array.Resize call above, you get an IndexOutOfRangeException.

Array.Resize always copies

Programming tip

When doing performance analysis, using IL Disassembler to see what the framework methods do is important. The Array.Resize method always copies the array, unless it throws an exception. Most calls will internally result in an Array.Copy call, such as this: "Copy(sourceArray, 0, destinationArray, 0, SIZE);".

Array.Copy Method Usage IL Disassembler Tutorial

Performance

Performance optimization

Clearly, copying your array completely when resizing it is wasteful in many situations. For these cases, use ArrayList or preferably List. However, sometimes Array.Resize can lead to better performance, because using arrays results in excellent memory efficiency and element access speed.

List Examples

I have used this method to improve performance in a custom hashtable, which could use arrays of buckets and not Lists. This reduced memory usage, and in the end enhanced performance significantly.

Generic

Generic type

Whenever you see a method that contains angle brackets < > before it the parameters, this is a generic method. With Array.Resize, you do not need to type the brackets. Example 2 shows how you can specify the type in brackets.

Generic Method

Summary

Here we saw ways to use Array.Resize to expand and contract arrays. This is useful only when arrays are required for some reason. You can use Array.Resize to avoid using Lists to reduce memory usage. However, in many programs it causes a needless performance problem.

Array Types
.NET