Array::CreateInstance Method (Type, array<Int32>, array<Int32>)
Assembly: mscorlib (in mscorlib.dll)
public: static Array^ CreateInstance( Type^ elementType, array<int>^ lengths, array<int>^ lowerBounds )
Parameters
- elementType
- Type: System::Type
The Type of the Array to create.
- lengths
- Type: array<System::Int32>
A one-dimensional array that contains the size of each dimension of the Array to create.
- lowerBounds
- Type: array<System::Int32>
A one-dimensional array that contains the lower bound (starting index) of each dimension of the Array to create.
Return Value
Type: System::ArrayA new multidimensional Array of the specified Type with the specified length and lower bound for each dimension.
| Exception | Condition |
|---|---|
| ArgumentNullException | elementType is nullptr. -or- lengths is nullptr. -or- lowerBounds is nullptr. |
| ArgumentException | elementType is not a valid Type. -or- The lengths array contains less than one element. -or- The lengths and lowerBounds arrays do not contain the same number of elements. |
| NotSupportedException | elementType is not supported. For example, Void is not supported. -or- elementType is an open generic type. |
| ArgumentOutOfRangeException | Any value in lengths is less than zero. -or- Any value in lowerBounds is very large, such that the sum of a dimension's lower bound and length is greater than Int32::MaxValue. |
Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.
The lengths and lowerBounds arrays must have the same number of elements. The number of elements in the lengths array must equal the number of dimensions in the new Array.
Each element of the lengths array must specify the length of the corresponding dimension in the new Array.
Each element of the lowerBounds array must specify the lower bound of the corresponding dimension in the new Array. Generally, the .NET Framework class library and many programming languages do not handle nonzero lower bounds.
Reference-type elements are initialized to nullptr. Value-type elements are initialized to zero.
This method is an O(n) operation, where n is the product of all values in lengths.
The following code example shows how to create and initialize a multidimensional Array with specified lower bounds.
using namespace System; void PrintValues( Array^ myArr ); void main() { // Creates and initializes a multidimensional Array instance of type String. array<int>^myLengthsArray = {3,5}; array<int>^myBoundsArray = {2,3}; Array^ myArray = Array::CreateInstance( String::typeid, myLengthsArray, myBoundsArray ); for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ ) for ( int j = myArray->GetLowerBound( 1 ); j <= myArray->GetUpperBound( 1 ); j++ ) { array<int>^myIndicesArray = {i,j}; myArray->SetValue( String::Concat( Convert::ToString( i ), j ), myIndicesArray ); } // Displays the lower bounds and the upper bounds of each dimension. Console::WriteLine( "Bounds:\tLower\tUpper" ); for ( int i = 0; i < myArray->Rank; i++ ) Console::WriteLine( "{0}:\t{1}\t{2}", i, myArray->GetLowerBound( i ), myArray->GetUpperBound( i ) ); // Displays the values of the Array. Console::WriteLine( "The Array instance contains the following values:" ); PrintValues( myArray ); } void PrintValues( Array^ myArr ) { System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator(); int i = 0; int cols = myArr->GetLength( myArr->Rank - 1 ); while ( myEnumerator->MoveNext() ) { if ( i < cols ) { i++; } else { Console::WriteLine(); i = 1; } Console::Write( "\t{0}", myEnumerator->Current ); } Console::WriteLine(); } /* This code produces the following output. Bounds: Lower Upper 0: 2 4 1: 3 7 The Array instance contains the following values: 23 24 25 26 27 33 34 35 36 37 43 44 45 46 47 */
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.