Mono Class Library: System.Array Overview | Members

System.Array.CreateInstance Method

Creates a multidimensional array whose element type is the specified Type, and dimension lengths and lower bounds, as specified. [Edit]

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

Parameters

elementType
The Type of the elements contained in the new Array instance. [Edit]
lengths
A one-dimensional array of int objects that contains the size of each dimension of the new Array instance. [Edit]
lowerBounds
A one-dimensional array of int objects that contains the lower bound of each dimension of the new Array instance. [Edit]

Returns

A new multidimensional Array whose element type is the specified Type and with the specified length and lower bound for each dimension. [Edit]

Exceptions

TypeReason
ArgumentNullExceptionelementType, lengths, or lowerBounds is null. [Edit]
ArgumentException

elementType is not a valid Type.

-or-

lengths.Length = 0.

-or-

lengths and lowerBounds do not contain the same number of elements.

[Edit]
ArgumentOutOfRangeExceptionA value in lengths is less than zero. [Edit]

Remarks

The lengths and lowerBounds are required to have the same number of elements. The number of elements in lengths equals the number of dimensions in the new Array instance

Each element of lengths specifies the length of the corresponding dimension in the new Array instance.

Each element of lowerBounds specifies the lower bound of the corresponding dimension in the new Array instance.

Reference-type elements will be set to null. Value-type elements will be set to zero, except for bool elements, which will be set to false.

Note: Unlike most classes, Array provides the Array.CreateInstance(Type, int) method, instead of public constructors, to allow for late bound access.

[Edit]

Example

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

C# Example
using System;

public class MultiDimNonZeroBoundExample
{
   public static void Main()
   {
      int i, j, k;
      int[] indexAry = {4, 2, 3};
      int[] lowboundAry = {3, 2, 1};
      Array ary = Array.CreateInstance( typeof(int), indexAry, lowboundAry );
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++ )
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++ )
         {
            for( k = ary.GetLowerBound(2); k <= ary.GetUpperBound(2); k++ )
            {
               ary.SetValue( (100*i + 10*j + k), i, j, k );
            }
         }
      }
      Console.WriteLine("The elements of the array are:");
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++)
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++)
         {
             for( k = ary.GetLowerBound(2); k <= ary.GetUpperBound(2); k++ )
            {
               Console.Write("{0, 3} ", ary.GetValue(i, j, k));
            }
            Console.WriteLine();
         }
         Console.WriteLine();
      }
   }
}
   

The output is

Example
The elements of the array are:
321 322 323
331 332 333

421 422 423
431 432 433

521 522 523
531 532 533

621 622 623
631 632 633

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0