C# Array.Resize

Convert or change

An array can be resized. With Array.Resize, we make it bigger or smaller, holding more or fewer elements. This uses memory efficiently—but can be slow to execute. We show how Resize reallocates and copies the elements.

Array.Copy Method

Example

One: 1

This first example shows how to 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 number of bytes or elements.

Here:Array.Resize changes an array with four elements to one with two elements. It reduces the size of the 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
Framework: NET

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.

Example 2

Here we look at an example of how to 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.

Note:I have used this in implementing hash table buckets. Array.Resize is helpful in optimization tasks.

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
    (Now has 6 elements.)

python

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

IndexOutOfRangeException

Copy

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 invoke Array.Copy.

IL Disassembler Tutorial

Performance

Performance optimization

Copying your array completely when resizing it is wasteful in many situations. For these cases, use List. But sometimes Array.Resize can lead to better performance. Arrays boost memory efficiency and lookup speed.

List Examples

Note:I have used this method to improve performance in a custom hashtable, which could use arrays of buckets and not Lists.

And:This reduced memory usage.
In the end, it significantly enhanced performance.

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. The second example shows how to specify the type in brackets.

Generic Method

Summary

The C# programming language

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 Array.Resize causes a needless performance problem. Where possible, it should be avoided.


C#: Array