Export (0) Print
Expand All
2 out of 9 rated this helpful - Rate this topic

Array.Resize<T> Method

Changes the number of elements of a one-dimensional array to the specified new size.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static void Resize<T>(
	ref T[] array,
	int newSize
)

Type Parameters

T

The type of the elements of the array.

Parameters

array
Type: T[]

The one-dimensional, zero-based array to resize, or null to create a new array with the specified size.

newSize
Type: System.Int32

The size of the new array.

ExceptionCondition
ArgumentOutOfRangeException

newSize is less than zero.

This 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. array must be a one-dimensional array.

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 ignored. If newSize is equal to the Length of the old array, this method does nothing.

This method is an O(n) operation, where n is newSize.

The Resize<T> method resizes a one-dimensional array only. The Array class does not include a method for resizing multi-dimensional arrays. To do this, you must either provide your own code or call a special-purpose method in a third-party library. The following code illustrates one possible implementation for a method that resizes an array of n dimensions.

using System;

public class Example
{
   public static void Main()
   {
      int[,] arr = new int[10,2];
      for (int n1 = 0; n1 <= arr.GetUpperBound(0); n1++) {
         arr[n1, 0] = n1;
         arr[n1, 1] = n1 * 2;
      } 

      // Make a 2-D array larger in the first dimension.
      arr = (int[,]) ResizeArray(arr, new int[] { 12, 2} );
      for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++) 
         Console.WriteLine("{0}: {1}, {2}", ctr, arr[ctr, 0], arr[ctr, 1]);
      Console.WriteLine();


      // Make a 2-D array smaller in the first dimension.
      arr = (int[,]) ResizeArray(arr, new int[] { 2, 2} );
      for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++) 
         Console.WriteLine("{0}: {1}, {2}", ctr, arr[ctr, 0], arr[ctr, 1]);
   }

   private static Array ResizeArray(Array arr, int[] newSizes)
   {
      if (newSizes.Length != arr.Rank)
         throw new ArgumentException("arr must have the same number of dimensions " +
                                     "as there are elements in newSizes", "newSizes"); 

      var temp = Array.CreateInstance(arr.GetType().GetElementType(), newSizes);
      int length = arr.Length <= temp.Length ? arr.Length : temp.Length;
      Array.ConstrainedCopy(arr, 0, temp, 0, length);
      return temp;
   }   
}
// The example displays the following output: 
//       0: 0, 0 
//       1: 1, 2 
//       2: 2, 4 
//       3: 3, 6 
//       4: 4, 8 
//       5: 5, 10 
//       6: 6, 12 
//       7: 7, 14 
//       8: 8, 16 
//       9: 9, 18 
//       10: 0, 0 
//       11: 0, 0 
//        
//       0: 0, 0 
//       1: 1, 2

The following example shows how resizing affects the array.

using System;

public class SamplesArray  
{
    public static void Main()  {

        // Create and initialize a new string array.
        String[] myArr = {"The", "quick", "brown", "fox", "jumps", 
            "over", "the", "lazy", "dog"};

        // Display the values of the array.
        Console.WriteLine( 
            "The string array initially contains the following values:");
        PrintIndexAndValues(myArr);

        // Resize the array to a bigger size (five elements larger).
        Array.Resize(ref myArr, myArr.Length + 5);

        // Display the values of the array.
        Console.WriteLine("After resizing to a larger size, ");
        Console.WriteLine("the string array contains the following values:");
        PrintIndexAndValues(myArr);

        // Resize the array to a smaller size (four elements).
        Array.Resize(ref myArr, 4);

        // Display the values of the array.
        Console.WriteLine("After resizing to a smaller size, ");
        Console.WriteLine("the string array contains the following values:");
        PrintIndexAndValues(myArr);
    }

    public static void PrintIndexAndValues(String[] myArr)  {
        for(int i = 0; i < myArr.Length; i++)  
        {
            Console.WriteLine("   [{0}] : {1}", i, myArr[i]);
        }
        Console.WriteLine();
    }
}

/* 
This code produces the following output.

The string array initially contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox
   [4] : jumps
   [5] : over
   [6] : the
   [7] : lazy
   [8] : dog

After resizing to a larger size, 
the string array contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox
   [4] : jumps
   [5] : over
   [6] : the
   [7] : lazy
   [8] : dog
   [9] :
   [10] :
   [11] :
   [12] :
   [13] :

After resizing to a smaller size, 
the string array contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox

*/

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft. All rights reserved.