TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Array.Resize Examples

These C# example programs use the Array.Resize method. Resize creates a new array and copies existing elements to it.

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.

Array.Copy

Based on:

.NET 4.5

Example. This first example uses 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.

Here: Array.Resize changes an array with four elements to one with two elements.

C# program that uses Array.Resize

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

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.

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

python

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.

IL Disassembler

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.

List

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.

Generic Method

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf