Array.Resize 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. We show how Resize reallocates and copies elements.

Internals. Internally, the single call to Array.Resize above 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.

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

Example 2. Next, we replace an array with a larger one 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.

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

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

Performance. 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.

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. 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.

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. We can use Array.Resize to avoid using Lists—this may help performance.