Home
Search
C# Array.Resize ExamplesUse the Array.Resize method. Resize creates a new array and copies existing elements to it.
dot net perls
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.Copy
Array
Shrink 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 part.
Step 1 We initialize an array of ints. It has 4 elements, and we assign them all to integer 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. int[] array = new int[4]; array[0] = 10; array[1] = 20; array[2] = 30; array[3] = 40; for (int i = 0; i < array.Length; i++) { Console.WriteLine("BEFORE: {0}", array[i]); } // Step 2: resize the array from 4 to 2 elements. Array.Resize(ref array, 2); for (int i = 0; i < array.Length; i++) { Console.WriteLine("AFTER: {0}", array[i]); } } }
BEFORE: 10 BEFORE: 20 BEFORE: 30 BEFORE: 40 AFTER: 10 AFTER: 20
Grow example. 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
C# program that grows array with Array.Resize
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)); } }
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.
Exception
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.
Home
© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.