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, Int64[])

Creates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. The dimension lengths are specified in an array of 64-bit integers.

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

public static Array CreateInstance (
	Type elementType,
	params long[] lengths
)
public static Array CreateInstance (
	Type elementType, 
	long[] lengths
)
public static function CreateInstance (
	elementType : Type, 
	... lengths : long[]
) : Array

Parameters

elementType

The Type of the Array to create.

lengths

An array of 64-bit integers that represent the size of each dimension of the Array to create. Each integer in the array must be between zero and Int32.MaxValue, inclusive.

Return Value

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

Exception typeCondition

ArgumentNullException

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

-or-

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

ArgumentException

elementType is not a valid Type.

-or-

The lengths array contains less than one element.

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 greater than Int32.MaxValue.

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

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.

Reference-type elements are initialized to a 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.

The following code example shows how to create and initialize a multidimensional Array.

using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a multidimensional Array of type String.
      int[] myLengthsArray = new int[4] { 2, 3, 4, 5 };
      Array my4DArray=Array.CreateInstance( typeof(String), myLengthsArray );
      for ( int i = my4DArray.GetLowerBound(0); i <= my4DArray.GetUpperBound(0); i++ )
         for ( int j = my4DArray.GetLowerBound(1); j <= my4DArray.GetUpperBound(1); j++ )
            for ( int k = my4DArray.GetLowerBound(2); k <= my4DArray.GetUpperBound(2); k++ )
               for ( int l = my4DArray.GetLowerBound(3); l <= my4DArray.GetUpperBound(3); l++ )  {
                  int[] myIndicesArray = new int[4] { i, j, k, l };
                  my4DArray.SetValue( Convert.ToString(i) + j + k + l, myIndicesArray );
               }

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


   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.

The four-dimensional Array contains the following values:
    0000    0001    0002    0003    0004
    0010    0011    0012    0013    0014
    0020    0021    0022    0023    0024
    0030    0031    0032    0033    0034
    0100    0101    0102    0103    0104
    0110    0111    0112    0113    0114
    0120    0121    0122    0123    0124
    0130    0131    0132    0133    0134
    0200    0201    0202    0203    0204
    0210    0211    0212    0213    0214
    0220    0221    0222    0223    0224
    0230    0231    0232    0233    0234
    1000    1001    1002    1003    1004
    1010    1011    1012    1013    1014
    1020    1021    1022    1023    1024
    1030    1031    1032    1033    1034
    1100    1101    1102    1103    1104
    1110    1111    1112    1113    1114
    1120    1121    1122    1123    1124
    1130    1131    1132    1133    1134
    1200    1201    1202    1203    1204
    1210    1211    1212    1213    1214
    1220    1221    1222    1223    1224
    1230    1231    1232    1233    1234
*/

import System.*;

public class SamplesArray
{
    public static void main(String[] args)
    {
        // Creates and initializes a multidimensional Array of type String.
        int myLengthsArray[] = new int[] { 2, 3, 4, 5 };
        Array my4DArray = Array.CreateInstance(String.class.ToType(), 
            myLengthsArray);
        for (int i = my4DArray.GetLowerBound(0); i <= my4DArray.
            GetUpperBound(0); i++) {
            for (int j = my4DArray.GetLowerBound(1); j <= my4DArray.
                GetUpperBound(1); j++) {
                for (int k = my4DArray.GetLowerBound(2); k <= my4DArray.
                    GetUpperBound(2); k++) {
                    for (int l = my4DArray.GetLowerBound(3); l <= my4DArray.
                        GetUpperBound(3); l++) {
                        int myIndicesArray[] = new int[] { i, j, k, l };
                        my4DArray.SetValue(Convert.ToString(i) + j + k + l,
                            myIndicesArray);
                    }
                }
            }  // Displays the values of the Array.
        }
        Console.WriteLine("The four-dimensional Array contains" 
            + " the following values:");
        PrintValues(my4DArray);
    } //main

    public static void PrintValues(Array myArr)
    {
        System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
        int i = 0;
        int cols = myArr.GetLength(myArr.get_Rank() - 1);
        while (myEnumerator.MoveNext()) {
            if (i < cols) {
                i++;
            }
            else {
                Console.WriteLine();
                i = 1;
            }
            Console.Write("\t{0}", myEnumerator.get_Current());
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArray

/*
 This code produces the following output.
 
 The four-dimensional Array contains the following values:
     0000    0001    0002    0003    0004
     0010    0011    0012    0013    0014
     0020    0021    0022    0023    0024
     0030    0031    0032    0033    0034
     0100    0101    0102    0103    0104
     0110    0111    0112    0113    0114
     0120    0121    0122    0123    0124
     0130    0131    0132    0133    0134
     0200    0201    0202    0203    0204
     0210    0211    0212    0213    0214
     0220    0221    0222    0223    0224
     0230    0231    0232    0233    0234
     1000    1001    1002    1003    1004
     1010    1011    1012    1013    1014
     1020    1021    1022    1023    1024
     1030    1031    1032    1033    1034
     1100    1101    1102    1103    1104
     1110    1111    1112    1113    1114
     1120    1121    1122    1123    1124
     1130    1131    1132    1133    1134
     1200    1201    1202    1203    1204
     1210    1211    1212    1213    1214
     1220    1221    1222    1223    1224
     1230    1231    1232    1233    1234
 */

import System;

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

// Displays the values of the Array.
Console.WriteLine( "The four-dimensional Array contains the following values:" );
PrintValues( my4DArray );
 
 
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.
 
 The four-dimensional Array contains the following values:
     0000    0001    0002    0003    0004
     0010    0011    0012    0013    0014
     0020    0021    0022    0023    0024
     0030    0031    0032    0033    0034
     0100    0101    0102    0103    0104
     0110    0111    0112    0113    0114
     0120    0121    0122    0123    0124
     0130    0131    0132    0133    0134
     0200    0201    0202    0203    0204
     0210    0211    0212    0213    0214
     0220    0221    0222    0223    0224
     0230    0231    0232    0233    0234
     1000    1001    1002    1003    1004
     1010    1011    1012    1013    1014
     1020    1021    1022    1023    1024
     1030    1031    1032    1033    1034
     1100    1101    1102    1103    1104
     1110    1111    1112    1113    1114
     1120    1121    1122    1123    1124
     1130    1131    1132    1133    1134
     1200    1201    1202    1203    1204
     1210    1211    1212    1213    1214
     1220    1221    1222    1223    1224
     1230    1231    1232    1233    1234
 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft