Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Array::CreateInstance Method (Type^, Int32, Int32)

 

Creates a two-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public:
static Array^ CreateInstance(
	Type^ elementType,
	int length1,
	int length2
)

Parameters

elementType
Type: System::Type^

The Type of the Array to create.

length1
Type: System::Int32

The size of the first dimension of the Array to create.

length2
Type: System::Int32

The size of the second dimension of the Array to create.

Return Value

Type: System::Array^

A new two-dimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

Exception Condition
ArgumentNullException

elementType is null.

ArgumentException

elementType is not a valid Type.

NotSupportedException

elementType is not supported. For example, Void is not supported.

-or-

elementType is an open generic type.

ArgumentOutOfRangeException

length1 is less than zero.

-or-

length2 is less than zero.

Unlike most classes, Array provides theCreateInstance method, instead of public constructors, to allow for late bound access.

Reference-type elements are initialized to null. Value-type elements are initialized to zero.

This method is an O(n) operation, where n is the product of length1 and length2.

The following code example shows how to create and initialize a two-dimensional Array.

using namespace System;
void PrintValues( Array^ myArr );
void main()
{
   // Creates and initializes a two-dimensional Array instance of type String.
   Array^ my2DArray = Array::CreateInstance( String::typeid, 2, 3 );
   for ( int i = my2DArray->GetLowerBound( 0 ); i <= my2DArray->GetUpperBound( 0 ); i++ )
      for ( int j = my2DArray->GetLowerBound( 1 ); j <= my2DArray->GetUpperBound( 1 ); j++ )
         my2DArray->SetValue( String::Concat( "abc", i, j ), i, j );

   // Displays the values of the Array.
   Console::WriteLine(  "The two-dimensional Array instance contains the following values:" );
   PrintValues( my2DArray );
}

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.

 The two-dimensional Array instance contains the following values:
     abc00    abc01    abc02
     abc10    abc11    abc12
 */

.NET Framework
Available since 1.1
Return to top
Show:
© 2017 Microsoft