Export (0) Print
Expand All

Array.CreateInstance Method (Type, Int32, Int32, Int32)

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

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

public static Array CreateInstance (
	Type elementType,
	int length1,
	int length2,
	int length3
)
public static Array CreateInstance (
	Type elementType, 
	int length1, 
	int length2, 
	int length3
)
public static function CreateInstance (
	elementType : Type, 
	length1 : int, 
	length2 : int, 
	length3 : int
) : Array
Not applicable.

Parameters

elementType

The Type of the Array to create.

length1

The size of the first dimension of the Array to create.

length2

The size of the second dimension of the Array to create.

length3

The size of the third dimension of the Array to create.

Return Value

A new three-dimensional Array of the specified Type with the specified length for each dimension, 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

length1 is less than zero.

-or-

length2 is less than zero.

-or-

length3 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 the product of length1, length2, and length3.

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

using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a three-dimensional Array of type Object.
      Array my3DArray=Array.CreateInstance( typeof(Object), 2, 3, 4 );
      for ( int i = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++ )
         for ( int j = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++ )
            for ( int k = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++ )
               my3DArray.SetValue( "abc" + i + j + k, i, j, k );

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


   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 three-dimensional Array contains the following values:
    abc000    abc001    abc002    abc003
    abc010    abc011    abc012    abc013
    abc020    abc021    abc022    abc023
    abc100    abc101    abc102    abc103
    abc110    abc111    abc112    abc113
    abc120    abc121    abc122    abc123
*/

import System.*;

public class SamplesArray
{
    public static void main(String[] args)
    {
        // Creates and initializes a three-dimensional Array of type Object.
        Array my3DArray = Array.CreateInstance(Object.class.ToType(), 2, 3, 4);
        for (int i = my3DArray.GetLowerBound(0); i <= my3DArray.
            GetUpperBound(0); i++) {
            for (int j = my3DArray.GetLowerBound(1); j <= my3DArray.
                GetUpperBound(1); j++) {
                for (int k = my3DArray.GetLowerBound(2); k <= my3DArray.
                    GetUpperBound(2); k++) {
                    my3DArray.SetValue("abc" + i + j + k, i, j, k);
                }
            }  // Displays the values of the Array.
        }
        Console.WriteLine("The three-dimensional Array contains the"
            + " following values:");
        PrintValues(my3DArray);
    } //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 three-dimensional Array contains the following values:
     abc000    abc001    abc002    abc003
     abc010    abc011    abc012    abc013
     abc020    abc021    abc022    abc023
     abc100    abc101    abc102    abc103
     abc110    abc111    abc112    abc113
     abc120    abc121    abc122    abc123
 */

import System;

// Creates and initializes a three-dimensional Array of type Object.
var my3DArray : System.Array = System.Array.CreateInstance( System.Object, 2, 3, 4 );
for ( var i : int = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++ )
  for ( var j : int = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++ )
     for ( var k : int = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++ )
        my3DArray.SetValue( "abc" + i + j + k, i, j, k );

// Displays the values of the Array.
Console.WriteLine( "The three-dimensional Array contains the following values:" );
PrintValues( my3DArray );
  
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 three-dimensional Array contains the following values:
     abc000    abc001    abc002    abc003
     abc010    abc011    abc012    abc013
     abc020    abc021    abc022    abc023
     abc100    abc101    abc102    abc103
     abc110    abc111    abc112    abc113
     abc120    abc121    abc122    abc123
 */

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft