Array::Resize<T> Method
Changes the number of elements of an array to the specified new size.
Assembly: mscorlib (in mscorlib.dll)
Type Parameters
- T
The type of the elements of the array.
Parameters
- array
- Type: array<T>%
The one-dimensional, zero-based array to resize, or nullptr to create a new array with the specified size.
- newSize
- Type: System::Int32
The size of the new array.
| Exception | Condition |
|---|---|
| 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.
If array is nullptr, 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 following example shows how resizing affects the array.
using namespace System; static void PrintIndexAndValues(array<String^>^myArr) { for(int i = 0; i < myArr->Length; i++) { Console::WriteLine(L" [{0}] : {1}", i, myArr[i]); } Console::WriteLine(); } int main() { // Create and initialize a new string array. array<String^>^myArr = {L"The", L"quick", L"brown", L"fox", L"jumps", L"over", L"the", L"lazy", L"dog"}; // Display the values of the array. Console::WriteLine( L"The string array initially contains the following values:"); PrintIndexAndValues(myArr); // Resize the array to a bigger size (five elements larger). Array::Resize(myArr, myArr->Length + 5); // Display the values of the array. Console::WriteLine(L"After resizing to a larger size, "); Console::WriteLine(L"the string array contains the following values:"); PrintIndexAndValues(myArr); // Resize the array to a smaller size (four elements). Array::Resize(myArr, 4); // Display the values of the array. Console::WriteLine(L"After resizing to a smaller size, "); Console::WriteLine(L"the string array contains the following values:"); PrintIndexAndValues(myArr); return 1; } /* 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 */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.