Mono Class Library: System.Array Overview | Members

System.Array.CreateInstance Method

Creates a zero-based, three-dimensional array of the specified Type and dimension lengths.

public static Array CreateInstance (Type elementType, int length1, int length2, int length3)

Parameters

elementType
The Type of the elements contained in the new Array instance.
length1
A int that contains the number of elements contained in the first dimension of the new Array instance.
length2
A int that contains the number of elements contained in the second dimension of the new Array instance.
length3
A int that contains the number of elements contained in the third dimension of the new Array instance.

Returns

A new zero-based, three-dimensional Array instance of elementType objects with the size length1 for the first dimension, length2 for the second, and length3 for the third.

Exceptions

TypeReason
ArgumentNullExceptionelementType is null.
ArgumentExceptionelementType is not a valid Type.
ArgumentOutOfRangeException

length1 < 0.

-or-

length2 < 0.

-or-

length3 < 0.

Remarks

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.

Example

The following example shows how to create and initialize a three-dimensional Array.

C# Example

using System;

public class Create3DArrayExample
{
   public static void Main()
   {
      int i, j, k;
      Array ary = Array.CreateInstance( typeof(int), 2, 4, 3 );
      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:
  0   1   2
 10  11  12
 20  21  22
 30  31  32

100 101 102
110 111 112
120 121 122
130 131 132
 

Requirements

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