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


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

Public Shared Function CreateInstance (
	elementType As Type,
	length1 As Integer,
	length2 As Integer,
	length3 As Integer
) As Array


Type: System.Type

The Type of the Array to create.

Type: System.Int32

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

Type: System.Int32

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

Type: System.Int32

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

Return Value

Type: System.Array

A new three-dimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

Exception Condition

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.


length1 is less than zero.


length2 is less than zero.


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 null. 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.

Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    

    Public Shared Sub Main()

        ' Creates and initializes a three-dimensional Array of type Object.
        Dim my3DArray As Array = Array.CreateInstance(GetType(Object), 2, 3, 4)
        Dim i As Integer
        For i = my3DArray.GetLowerBound(0) To my3DArray.GetUpperBound(0)
            Dim j As Integer
            For j = my3DArray.GetLowerBound(1) To my3DArray.GetUpperBound(1)
                Dim k As Integer
                For k = my3DArray.GetLowerBound(2) To my3DArray.GetUpperBound(2)
                    my3DArray.SetValue("abc" + i.ToString() _
                       + j.ToString() + k.ToString(), i, j, k)
                Next k 
            Next j
        Next i

        ' Displays the values of the Array.
        Console.WriteLine("The three-dimensional Array contains the " _
           + "following values:")
    End Sub

    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
    End Sub
End Class

' 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 

.NET Framework
Available since 1.1
Return to top