ASP.NET Tutorial : C# Array Resizing with Examples

Leave a Comment

Using the Array.Resize<T> function in C#, you can resize an array. Using this method, you can set a new size for a one-dimensional array's element count. Let's examine its operation and look at a few instances.

The Array.Resize<T> Technique

The definition of the Array.Resize<T> method is as follows:

public static void Resize<T>(ref T[] array, int newSize);
  • T: The type of the elements in the array.
  • array: The one-dimensional, zero-based array to resize, or null to create a new array with the specified size.
  • newSize: The size of the new array.
Examples

Let’s look at some examples to understand how resizing affects an array.

1. Resizing to a Larger Size

Suppose we have the following string array:

string[] myArr = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" };

We can resize this array to a larger size (five elements larger):

Array.Resize(ref myArr, myArr.Length + 5);

After resizing, the array contains the following values:

2. Resizing to a Smaller Size

Now let’s resize the array to a smaller size (four elements):

Array.Resize(ref myArr, 4);

After resizing, the array contains the following values:

"The", "quick", "brown", "fox"
Remarks
  • The Array.Resize<T> method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.
  • If array is null, this method creates a new array with the specified size.
  • If newSize is greater than the length of the old array, a new array is allocated, and all the elements are copied from the old array to the new one.
  • If newSize is less than the length of the old array, a new array is allocated, and elements are copied from the old array to the new one until the new one is filled; the rest of the elements in the old array are ignored1.

Remember that Array.Resize<T> provides a convenient way to adjust the size of an array dynamically during program execution

Windows Hosting Recommendation

HostForLIFEASP.NET receives Spotlight standing advantage award for providing recommended, cheap and fast ecommerce Hosting including the latest Magento. From the leading technology company, Microsoft. All the servers are equipped with the newest Windows Server 2022 R2, SQL Server 2022, ASP.NET Core 7.0.10 , ASP.NET MVC, Silverlight 5, WebMatrix and Visual Studio Lightswitch. Security and performance are at the core of their Magento hosting operations to confirm every website and/or application hosted on their servers is highly secured and performs at optimum level. mutually of the European ASP.NET hosting suppliers, HostForLIFE guarantees 99.9% uptime and fast loading speed. From €3.49/month , HostForLIFE provides you with unlimited disk space, unlimited domains, unlimited bandwidth,etc, for your website hosting needs.
 
https://hostforlifeasp.net/
Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment