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


Creates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.

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

Public Shared Function CreateInstance (
	elementType As Type,
	lengths As Integer(),
	lowerBounds As Integer()
) As Array


Type: System.Type

The Type of the Array to create.

Type: System.Int32()

A one-dimensional array that contains the size of each dimension of the Array to create.

Type: System.Int32()

A one-dimensional array that contains the lower bound (starting index) of each dimension of the Array to create.

Return Value

Type: System.Array

A new multidimensional Array of the specified Type with the specified length and lower bound for each dimension.

Exception Condition

elementType is null.


lengths is null.


lowerBounds is null.


elementType is not a valid Type.


The lengths array contains less than one element.


The lengths and lowerBounds arrays do not contain the same number of elements.


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


elementType is an open generic type.


Any value in lengths is less than zero.


Any value in lowerBounds is very large, such that the sum of a dimension's lower bound and length is greater than Int32.MaxValue.

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

The lengths and lowerBounds arrays must have the same number of elements. The number of elements in the lengths array must equal the number of dimensions in the new Array.

Each element of the lengths array must specify the length of the corresponding dimension in the new Array.

Each element of the lowerBounds array must specify the lower bound of the corresponding dimension in the new Array. Generally, the .NET Framework class library and many programming languages do not handle nonzero lower bounds.

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 all values in lengths.

The following code example shows how to create and initialize a multidimensional Array with specified lower bounds.

Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    

    Public Shared Sub Main()

        ' Creates and initializes a multidimensional Array of type String.
        Dim myLengthsArray() As Integer = {3, 5}
        Dim myBoundsArray() As Integer = {2, 3}
        Dim myArray As Array = Array.CreateInstance(GetType(String), _
           myLengthsArray, myBoundsArray)
        Dim i, j As Integer
        Dim myIndicesArray() As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            For j = myArray.GetLowerBound(1) To myArray.GetUpperBound(1)
                myIndicesArray = New Integer() {i, j}
                myArray.SetValue(i.ToString() + j.ToString(), myIndicesArray)
            Next j
        Next i

        ' Displays the lower bounds and the upper bounds of each dimension.
        Console.WriteLine("Bounds:" + ControlChars.Tab + "Lower" _
           + ControlChars.Tab + "Upper")
        For i = 0 To myArray.Rank - 1
            Console.WriteLine("{0}:" + ControlChars.Tab + "{1}" _
               + ControlChars.Tab + "{2}", i, myArray.GetLowerBound(i), _
        Next i

        ' Displays the values of the Array.
        Console.WriteLine("The 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.
' Bounds:    Lower    Upper
' 0:    2    4
' 1:    3    7
' The Array contains the following values:
'     23    24    25    26    27
'     33    34    35    36    37
'     43    44    45    46    47 

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top