Mono Class Library: System.Array Overview | Members

System.Array.CreateInstance Method

Constructs a zero-based, one-dimensional array with the specified number of elements of the specified type. [Edit]

public static Array CreateInstance (Type elementType, int length)

Parameters

elementType
The Type of the elements contained in the new Array instance. [Edit]
length
A int that contains the number of elements contained in the new Array instance. [Edit]

Returns

A zero-based, one-dimensional Array object containing length elements of type elementType. [Edit]

Exceptions

TypeReason
ArgumentNullExceptionelementType is null. [Edit]
ArgumentExceptionelementType is not a valid Type. [Edit]
ArgumentOutOfRangeExceptionlength < 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 one-dimensional Array.

C# Example
using System;

public class ArrayCreateInstanceExample
{

   public static void Main()
   {

      Array intAry = Array.CreateInstance(typeof(int),5);
      for (int i=intAry.GetLowerBound(0);i<=intAry.GetUpperBound(0);i++)
         intAry.SetValue(i*3,i);
      Console.Write("The values of the array are:");
      foreach (int i in intAry)
         Console.Write("{0} ",i);
   
   }

}
   

The output is

The values of the array are: 0 3 6 9 12

Requirements

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