Array.CreateInstance Method (Type, array<Int32[])
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<SecuritySafeCriticalAttribute> _
Public Shared Function CreateInstance ( _
elementType As Type, _
ParamArray lengths As Integer() _
) As Array
[SecuritySafeCriticalAttribute]
public static Array CreateInstance(
Type elementType,
params int[] lengths
)
Parameters
- elementType
Type: System.Type
The Type of the Array to create.
- lengths
Type: array<System.Int32[]
An array of 32-bit integers that represent the size 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 for each dimension, using zero-based indexing.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | elementType is nulla null reference (Nothing in Visual Basic). -or- lengths is nulla null reference (Nothing in Visual Basic). |
ArgumentException | elementType is not a valid Type. -or- The lengths array contains less than one element. |
NotSupportedException | elementType is not supported. For example, Void is not supported. -or- elementType is an open generic type. |
ArgumentOutOfRangeException | Any value in lengths is less than zero. |
Remarks
Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.
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.
Reference-type elements are initialized to nulla 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 all values in lengths.
Examples
The following code example shows how to create and initialize a multidimensional Array.
Note: |
---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
Public Class Example
Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
' Creates and initializes a multidimensional Array of type String.
Dim myLengthsArray() As Integer = {2, 3, 4, 5}
Dim my4DArray As Array = Array.CreateInstance(GetType(String), myLengthsArray)
Dim i, j, k, l As Integer
Dim myIndicesArray() As Integer
For i = my4DArray.GetLowerBound(0) To my4DArray.GetUpperBound(0)
For j = my4DArray.GetLowerBound(1) To my4DArray.GetUpperBound(1)
For k = my4DArray.GetLowerBound(2) To my4DArray.GetUpperBound(2)
For l = my4DArray.GetLowerBound(3) To my4DArray.GetUpperBound(3)
myIndicesArray = New Integer() {i, j, k, l}
my4DArray.SetValue(Convert.ToString(i) + j.ToString() _
+ k.ToString() + l.ToString(), myIndicesArray)
Next l
Next k
Next j
Next i
' Displays the values of the Array.
outputBlock.Text &= "The four-dimensional Array contains the following values:" & vbCrLf
PrintValues(outputBlock, my4DArray)
End Sub
Public Shared Sub PrintValues(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal myArr As Array)
Dim myEnumerator As System.Collections.IEnumerator = myArr.GetEnumerator()
Dim i As Integer = 0
Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
While myEnumerator.MoveNext()
If i < cols Then
i += 1
Else
outputBlock.Text &= vbCrLf
i = 1
End If
outputBlock.Text += String.Format(ControlChars.Tab + "{0}", myEnumerator.Current)
End While
outputBlock.Text &= vbCrLf
End Sub
End Class
' This code produces the following output.
'
' The four-dimensional Array contains the following values:
' 0000 0001 0002 0003 0004
' 0010 0011 0012 0013 0014
' 0020 0021 0022 0023 0024
' 0030 0031 0032 0033 0034
' 0100 0101 0102 0103 0104
' 0110 0111 0112 0113 0114
' 0120 0121 0122 0123 0124
' 0130 0131 0132 0133 0134
' 0200 0201 0202 0203 0204
' 0210 0211 0212 0213 0214
' 0220 0221 0222 0223 0224
' 0230 0231 0232 0233 0234
' 1000 1001 1002 1003 1004
' 1010 1011 1012 1013 1014
' 1020 1021 1022 1023 1024
' 1030 1031 1032 1033 1034
' 1100 1101 1102 1103 1104
' 1110 1111 1112 1113 1114
' 1120 1121 1122 1123 1124
' 1130 1131 1132 1133 1134
' 1200 1201 1202 1203 1204
' 1210 1211 1212 1213 1214
' 1220 1221 1222 1223 1224
' 1230 1231 1232 1233 1234
using System;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
// Creates and initializes a multidimensional Array of type String.
int[] myLengthsArray = new int[4] { 2, 3, 4, 5 };
Array my4DArray = Array.CreateInstance(typeof(String), myLengthsArray);
for (int i = my4DArray.GetLowerBound(0); i <= my4DArray.GetUpperBound(0); i++)
for (int j = my4DArray.GetLowerBound(1); j <= my4DArray.GetUpperBound(1); j++)
for (int k = my4DArray.GetLowerBound(2); k <= my4DArray.GetUpperBound(2); k++)
for (int l = my4DArray.GetLowerBound(3); l <= my4DArray.GetUpperBound(3); l++)
{
int[] myIndicesArray = new int[4] { i, j, k, l };
my4DArray.SetValue(Convert.ToString(i) + j + k + l, myIndicesArray);
}
// Displays the values of the Array.
outputBlock.Text += "The four-dimensional Array contains the following values:" + "\n";
PrintValues(outputBlock, my4DArray);
}
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)
{
i++;
}
else
{
outputBlock.Text += "\n";
i = 1;
}
outputBlock.Text += String.Format("\t{0}", myEnumerator.Current);
}
outputBlock.Text += "\n";
}
}
/*
This code produces the following output.
The four-dimensional Array contains the following values:
0000 0001 0002 0003 0004
0010 0011 0012 0013 0014
0020 0021 0022 0023 0024
0030 0031 0032 0033 0034
0100 0101 0102 0103 0104
0110 0111 0112 0113 0114
0120 0121 0122 0123 0124
0130 0131 0132 0133 0134
0200 0201 0202 0203 0204
0210 0211 0212 0213 0214
0220 0221 0222 0223 0224
0230 0231 0232 0233 0234
1000 1001 1002 1003 1004
1010 1011 1012 1013 1014
1020 1021 1022 1023 1024
1030 1031 1032 1033 1034
1100 1101 1102 1103 1104
1110 1111 1112 1113 1114
1120 1121 1122 1123 1124
1130 1131 1132 1133 1134
1200 1201 1202 1203 1204
1210 1211 1212 1213 1214
1220 1221 1222 1223 1224
1230 1231 1232 1233 1234
*/
Version Information
Silverlight
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
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.