Mono Class Library: System.Array Overview | Members

System.Array.CreateInstance Method

Creates a zero-based, two-dimensional array of the specified Type and dimension lengths. [Edit]

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

Parameters

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

Returns

A new zero-indexed, two-dimensional Array instance of elementType objects with the size length1 for the first dimension and length2 for the second. [Edit]

Exceptions

TypeReason
ArgumentNullExceptionelementType is null. [Edit]
ArgumentExceptionelementType is not a valid Type. [Edit]
ArgumentOutOfRangeException

length1 < 0.

-or-

length2 < 0.

[Edit]

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.

[Edit]

Example

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

C# Example
using System;

public class Create2DArrayExample
{
   public static void Main()
   {
      int i, j;
      Array ary = Array.CreateInstance( typeof(int), 5, 3 );
      for( i = ary.GetLowerBound(0); i <= ary.GetUpperBound(0); i++ )
      {
         for( j = ary.GetLowerBound(1); j <= ary.GetUpperBound(1); j++ )
         {
            ary.SetValue( (10*i + j), i, j );
         }
      }
      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++)
         {
            Console.Write("{0, 2} ", ary.GetValue(i, j));
         }
         Console.WriteLine();
      }
   }
} 
  

The output is

Example
The elements of the array are:
 0  1  2
10 11 12
20 21 22
30 31 32
40 41 42
 

Requirements

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