CreateInstance Method (Type, Int32)
Collapse the table of content
Expand the table of content

Array.CreateInstance Method (Type, Int32)

Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static Array CreateInstance(
	Type elementType,
	int length


Type: System.Type
The Type of the Array to create.
Type: System.Int32
The size of the Array to create.

Return Value

Type: System.Array
A new one-dimensional Array of the specified Type with the specified length, using zero-based indexing.


elementType is null.


elementType is not a valid Type.


elementType is not supported. For example, Void is not supported.


elementType is an open generic type.


length is less than zero.

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

Reference-type elements are initialized to null. Value-type elements are initialized to zero.

This method is an O(n) operation, where n is length.

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

using System;
public class Example

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)

      // Creates and initializes a one-dimensional Array of type Int32.
      Array my1DArray = Array.CreateInstance(typeof(Int32), 5);
      for (int i = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0); i++)
         my1DArray.SetValue(i + 1, i);

      // Displays the values of the Array.
      outputBlock.Text += "The one-dimensional Array contains the following values:" + "\n";
      PrintValues(outputBlock, my1DArray);

   public static void PrintValues(System.Windows.Controls.TextBlock outputBlock, Array myArr)
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = myArr.GetLength(myArr.Rank - 1);
      while (myEnumerator.MoveNext())
         if (i < cols)
            outputBlock.Text += "\n";
            i = 1;
         outputBlock.Text += String.Format("\t{0}", myEnumerator.Current);
      outputBlock.Text += "\n";
This code produces the following output.

The one-dimensional Array contains the following values:
    1    2    3    4    5


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

© 2016 Microsoft