This documentation is archived and is not being maintained.

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
)
public static Array CreateInstance (
	Type elementType, 
	int length
)
public static function CreateInstance (
	elementType : Type, 
	length : int
) : Array

Parameters

elementType

The Type of the Array to create.

length

The size of the Array to create.

Return Value

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

Exception typeCondition

ArgumentNullException

elementType is a null reference (Nothing in Visual Basic).

ArgumentException

elementType is not a valid Type.

NotSupportedException

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

-or-

elementType is an open generic type.

ArgumentOutOfRangeException

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 a null reference (Nothing in Visual Basic). 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 SamplesArray  {

   public static void Main()  {

      // 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.
      Console.WriteLine( "The one-dimensional Array contains the following values:" );
      PrintValues( my1DArray );
   }


   public static void PrintValues( Array myArr )  {
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = myArr.GetLength( myArr.Rank - 1 );
      while ( myEnumerator.MoveNext() )  {
         if ( i < cols )  {
            i++;
         } else  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "\t{0}", myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
/*
This code produces the following output.

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

import System.*;

public class SamplesArray
{
    public static void main(String[] args)
    {
        // Creates and initializes a one-dimensional Array of type Int32.
        Array my1DArray = Array.CreateInstance(Int32.class.ToType(), 5);
        for (int i = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0);
            i++) {
            my1DArray.SetValue((Int32)(i + 1), i);
        }
        // Displays the values of the Array.
        Console.WriteLine("The one-dimensional Array contains the"
            + " following values:");
        PrintValues(my1DArray);
    } //main

    public static void PrintValues(Array myArr)
    {
        System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
        int i = 0;
        int cols = myArr.GetLength(myArr.get_Rank() - 1);
        while (myEnumerator.MoveNext()) {
            if (i < cols) {
                i++;
            }
            else {
                Console.WriteLine();
                i = 1;
            }
            Console.Write("\t{0}", myEnumerator.get_Current());
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArray

/*
 This code produces the following output.
 
 The one-dimensional Array contains the following values:
     1    2    3    4    5
 */

import System;

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

// Displays the values of the Array.
Console.WriteLine( "The one-dimensional Array contains the following values:" );
PrintValues( my1DArray );

 
function PrintValues( myArr : System.Array)  {
   var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator();
   var i : int = 0;
   var cols : int = myArr.GetLength( myArr.Rank - 1 );
   while ( myEnumerator.MoveNext() )  {
      if ( i < cols )  {
         i++;
      } else  {
         Console.WriteLine();
         i = 1;
      }
      Console.Write( "\t{0}", myEnumerator.Current );
   }
   Console.WriteLine();
}
 /*
 This code produces the following output.
 
 The one-dimensional Array contains the following values:
     1    2    3    4    5
 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: