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
)

Parameters

elementType
Type: System.Type

The Type of the Array to create.

lengths
Type: System.Int32[]

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

lowerBounds
Type: 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.

ExceptionCondition
ArgumentNullException

elementType is null.

-or-

lengths is null.

-or-

lowerBounds is null.

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

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.

.NET Framework

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

Community Additions

ADD
Show:
© 2014 Microsoft