Export (0) Print
Expand All

Array.CreateInstance Method (Type, Int32[], Int32[])

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

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

public static Array CreateInstance (
	Type elementType,
	int[] lengths,
	int[] lowerBounds
)
public static Array CreateInstance (
	Type elementType, 
	int[] lengths, 
	int[] lowerBounds
)
public static function CreateInstance (
	elementType : Type, 
	lengths : int[], 
	lowerBounds : int[]
) : Array

Parameters

elementType

The Type of the Array to create.

lengths

A one-dimensional array that contains the size of each dimension of the Array to create.

lowerBounds

A one-dimensional array that contains the lower bound (starting index) of each dimension of the Array to create.

Return Value

A new multidimensional Array of the specified Type with the specified length and lower bound for each dimension.

Exception typeCondition

ArgumentNullException

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

-or-

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

-or-

lowerBounds is a 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.

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 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 with specified lower bounds.

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
*/

import System.*;

public class SamplesArray
{
    public static void main(String[] args)
    {
        // Creates and initializes a multidimensional Array of type String.
        int myLengthsArray[] = new int[] { 3, 5 };
        int myBoundsArray[] = new int[] { 2, 3 };
        Array myArray = Array.CreateInstance(String.class.ToType(),
            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[] { 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.get_Rank(); i++) {
            Console.WriteLine("{0}:\t{1}\t{2}", (Int32)i, (Int32)myArray.
                GetLowerBound(i), (Int32)myArray.GetUpperBound(i));
        }
        // Displays the values of the Array.
        Console.WriteLine("The Array contains the following values:");
        PrintValues(myArray);
    } //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.
 
 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
 */

 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
 */

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, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft