HomeSearch

C# Array.Resize Examples

Use the Array.Resize method. Resize creates a new array and copies existing elements to it.
Array.Resize. This .NET method allocates a new array. It then copies existing element values to the new array. This logic is needed when an array's size is inadequate.
Notes, resizing. We show how Resize reallocates and copies elements. The arguments to Array.Resize are easy to understand (unless you are unfamiliar with the method).Array.CopyArray
An example. We use Array.Resize to replace a large array with a smaller one. This is useful if we have a large array of data, and want to only keep the first number of bytes or elements.

Step 1: We initialize an array of characters. It has 4 elements, and we assign them all to char values.

Char Array

Step 2: We invoke Array.Resize with argument of 2. This call changes an array of 4 elements to one with 2 elements.

So: We reduce the size of the array by 2 elements. Array.Resize can expand, or shrink an array's size.

C# program that uses Array.Resize using System; class Program { static void Main() { // Step 1: initialize array for example. char[] array = new char[4]; array[0] = 'p'; array[1] = 'e'; array[2] = 'r'; array[3] = 'l'; for (int i = 0; i < array.Length; i++) { Console.Write(array[i]); } Console.WriteLine(); // Step 2: resize the array from 4 to 2 elements. Array.Resize(ref array, 2); for (int i = 0; i < array.Length; i++) { Console.Write(array[i]); } Console.WriteLine(); } } Output perl pe
Example 2. Next we expand an array's size. 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.

Caution: If we omit the Array.Resize call, we will get an IndexOutOfRangeException. This exception should be avoided.

IndexOutOfRangeException
Program 2 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 a new array. Array.Resize<char>(ref arr, 6); // Assign the last element. arr[5] = 'n'; // Display the array. Console.WriteLine(new string(arr)); } } Output python
Notes, internals. A call to Array.Resize runs through an algorithm that determines that the array needs to be larger or smaller. It copies the array, and then changes the reference.

So: Array.Resize is a misnomer. It does not resize the array. It replaces the array with a new one of a different size.

Copy. In performance analysis, we can use IL Disassembler to see what the Framework methods do. Array.Resize always copies the array, unless it throws an exception.IL DisassemblerException

And: Most calls will internally invoke Array.Copy. So Array.Copy is the same thing as creating a new array, and copying elements to it.

Performance. Copying an array completely when resizing it is wasteful in many situations. For these cases, use List—call Add() to add to empty space on the end.List

But: Sometimes Array.Resize can lead to better performance. Arrays boost memory efficiency and lookup speed.

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. When you see a method that has angle brackets before its parameters, this is a generic method. With Array.Resize, the compiler can usually infer this part of the syntax.Generic Class, Method
A summary. Array.Resize does not change existing arrays. It allocates and copies elements into a new array. It is useful only when arrays are required.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls