.NET Framework Class Library
Array..::.CreateInstance Method (Type, array<Int32>[]()[], array<Int32>[]()[])

Creates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.

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

Visual Basic (Declaration)
Public Shared Function CreateInstance ( _
    elementType As Type, _
    lengths As Integer(), _
    lowerBounds As Integer() _
) As Array
Visual Basic (Usage)
Dim elementType As Type
Dim lengths As Integer()
Dim lowerBounds As Integer()
Dim returnValue As Array

returnValue = Array.CreateInstance(elementType, _
    lengths, lowerBounds)
C#
public static Array CreateInstance(
    Type elementType,
    int[] lengths,
    int[] lowerBounds
)
Visual C++
public:
static Array^ CreateInstance(
    Type^ elementType, 
    array<int>^ lengths, 
    array<int>^ lowerBounds
)
JScript
public static function CreateInstance(
    elementType : Type, 
    lengths : int[], 
    lowerBounds : int[]
) : Array

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..::.Array
A new multidimensional Array of the specified Type with the specified length and lower bound for each dimension.
Exceptions

ExceptionCondition
ArgumentNullException

elementType is nullNothingnullptra null reference (Nothing in Visual Basic).

-or-

lengths is nullNothingnullptra null reference (Nothing in Visual Basic).

-or-

lowerBounds is nullNothingnullptra null reference (Nothing in Visual Basic).

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.

Remarks

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 nullNothingnullptra null reference (Nothing in Visual Basic). Value-type elements are initialized to zero.

This method is an O(n) operation, where n is the product of all values in lengths.

Examples

The following code example shows how to create and initialize a multidimensional Array with specified lower bounds.

Visual Basic
Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    

    Public Shared Sub Main()

        ' Creates and initializes a multidimensional Array of type String.
        Dim myLengthsArray() As Integer = {3, 5}
        Dim myBoundsArray() As Integer = {2, 3}
        Dim myArray As Array = Array.CreateInstance(GetType(String), _
           myLengthsArray, myBoundsArray)
        Dim i, j As Integer
        Dim myIndicesArray() As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            For j = myArray.GetLowerBound(1) To myArray.GetUpperBound(1)
                myIndicesArray = New Integer() {i, j}
                myArray.SetValue(i.ToString() + j.ToString(), myIndicesArray)
            Next j
        Next i

        ' Displays the lower bounds and the upper bounds of each dimension.
        Console.WriteLine("Bounds:" + ControlChars.Tab + "Lower" _
           + ControlChars.Tab + "Upper")
        For i = 0 To myArray.Rank - 1
            Console.WriteLine("{0}:" + ControlChars.Tab + "{1}" _
               + ControlChars.Tab + "{2}", i, myArray.GetLowerBound(i), _
               myArray.GetUpperBound(i))
        Next i

        ' Displays the values of the Array.
        Console.WriteLine("The Array contains the following values:")
        PrintValues(myArray)
    End Sub    

    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Bounds:    Lower    Upper
' 0:    2    4
' 1:    3    7
' The Array contains the following values:
'     23    24    25    26    27
'     33    34    35    36    37
'     43    44    45    46    47 
C#
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a multidimensional Array of type String.
      int[] myLengthsArray = new int[2] { 3, 5 };
      int[] myBoundsArray = new int[2] { 2, 3 };
      Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ )  {
            int[] myIndicesArray = new int[2] { i, j };
            myArray.SetValue( 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 contains the following values:" );
      PrintValues( myArray );
   }


   public static 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 contains the following values:
    23    24    25    26    27
    33    34    35    36    37
    43    44    45    46    47
*/
Visual C++
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
 */
JScript
 import System;

// Creates and initializes a multidimensional Array of type String.
var myLengthsArray : int[] = [ 3, 5 ];
var myBoundsArray : int[]  = [ 2, 3 ];
var myArray : System.Array = System.Array.CreateInstance( System.String, myLengthsArray, myBoundsArray );
for ( var i : int = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
  for ( var j : int = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ )  {
     var myIndicesArray : int[] = [i, j ];
     myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
  }

// Displays the lower bounds and the upper bounds of each dimension.
Console.WriteLine( "Bounds:\tLower\tUpper" );
for ( var k : int = 0; k < myArray.Rank; k++ )
  Console.WriteLine( "{0}:\t{1}\t{2}", k, myArray.GetLowerBound(k), myArray.GetUpperBound(k) );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintValues( myArray );


function PrintValues( myArr : System.Array)  {
   var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator();
   var i : int = 0;
   var cols : int = 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 contains the following values:
     23    24    25    26    27
     33    34    35    36    37
     43    44    45    46    47
 */
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Page view tracker