System Namespace Array Class
Creates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.
[ VB ]
<Serializable>
Overloads Public Shared Function CreateInstance ( _
ByVal elementType As Type, _
ByVal lengths ( ) As Integer, _
ByVal lowerBounds ( ) As Integer _
) As Array
[ C# ]
[Serializable]
public static Array CreateInstance (
Type elementType,
int [ ] lengths,
int [ ] lowerBounds
);
[ C++ ]
[Serializable]
public: static Array* CreateInstance (
Type* elementType,
int lengths __gc [ ],
int lowerBounds __gc [ ]
);
[JScript ]
public Serializable
static function CreateInstance (
elementType : Type,
lengths : int [ ],
lowerBounds : int [ ]
) : Array;
- elementType
- The Type of the Array to create.
- lengths
- A one-dimensional array that contains the size of each dimension of the Array to create.
- lowerBounds
- A one-dimensional array that contains the lower bound ( starting index ) of each dimension of the Array to create.
A new multidimensional Array of the specified Type with the specified length and lower bound for each dimension.
Exception Type |
Condition |
ArgumentNullException |
elementType is a null reference ( Nothing in Visual Basic ).
-or-
lengths is a null reference ( Nothing ).
-or-
lowerBounds is a null reference ( Nothing ).
|
ArgumentException |
elementType is not a valid Type.
-or-
The lengths array contains less than one element.
-or-
The lengths and lowerBounds arrays do not contain the same number of elements.
|
ArgumentOutOfRangeException |
Any value in lengths is less than zero. |
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 a null reference ( Nothing in Visual Basic ). Value-type elements are initialized to zero.
The following code example shows how to create and initialize a multidimensional Array with specified lower bounds.
[ VB ]
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.
Response.WriteLine ( "Bounds:" + ControlChars.Tab + "Lower" _
+ ControlChars.Tab + "Upper" )
For i = 0 To myArray.Rank - 1
Response.WriteLine ( "{0}:" + ControlChars.Tab + "{1}" _
+ ControlChars.Tab + "{2}", i, myArray.GetLowerBound ( i ) , _
myArray.GetUpperBound ( i ) )
Next i
' Displays the values of the Array.
Response.WriteLine ( "The Array contains the following values:" )
PrintValues ( myArray )
End Sub
Public Shared Sub PrintValues ( 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
Response.WriteLine ( )
i = 1
End If
Response.Write ( ControlChars.Tab + "{0}", myEnumerator.Current )
End While
Response.WriteLine ( )
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
[ C# ]
using System;
public class SamplesArray {
public static void Main ( ) {
// Creates and initializes a multidimensional Array of type String.
int [ ] myLengthsArray = new int[2] { 3, 5 };
int [ ] myBoundsArray = new int[2] { 2, 3 };
Array myArray=Array.CreateInstance ( typeof ( String ) , myLengthsArray, myBoundsArray );
for ( int i = myArray.GetLowerBound ( 0 ); i <= myArray.GetUpperBound ( 0 ); i++ )
for ( int j = myArray.GetLowerBound ( 1 ); j <= myArray.GetUpperBound ( 1 ); j++ ) {
int [ ] myIndicesArray = new int[2] { i, j };
myArray.SetValue ( Convert.ToString ( i ) + j, myIndicesArray );
}
// Displays the lower bounds and the upper bounds of each dimension.
Response.WriteLine ( "Bounds:\tLower\tUpper" );
for ( int i = 0; i < myArray.Rank; i++ )
Response.WriteLine ( "{0}:\t{1}\t{2}", i, myArray.GetLowerBound ( i ) , myArray.GetUpperBound ( i ) );
// Displays the values of the Array.
Response.WriteLine ( "The Array contains the following values:" );
PrintValues ( myArray );
}
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 {
Response.WriteLine ( );
i = 1;
}
Response.Write ( "\t{0}", myEnumerator.Current );
}
Response.WriteLine ( );
}
}
/*
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
*/
[JScript ]
import System;
// Creates and initializes a multidimensional Array of type String.
var myLengthsArray : int [ ] = [ 3, 5 ];
var myBoundsArray : int [ ] = [ 2, 3 ];
var myArray : System.Array = System.Array.CreateInstance ( System.String, myLengthsArray, myBoundsArray );
for ( var i : int = myArray.GetLowerBound ( 0 ); i <= myArray.GetUpperBound ( 0 ); i++ )
for ( var j : int = myArray.GetLowerBound ( 1 ); j <= myArray.GetUpperBound ( 1 ); j++ ) {
var myIndicesArray : int [ ] = [i, j ];
myArray.SetValue ( Convert.ToString ( i ) + j, myIndicesArray );
}
// Displays the lower bounds and the upper bounds of each dimension.
Response.WriteLine ( "Bounds:\tLower\tUpper" );
for ( var k : int = 0; k < myArray.Rank; k++ )
Response.WriteLine ( "{0}:\t{1}\t{2}", k, myArray.GetLowerBound ( k ) , myArray.GetUpperBound ( k ) );
// Displays the values of the Array.
Response.WriteLine ( "The Array contains the following values:" );
PrintValues ( myArray );
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 {
Response.WriteLine ( );
i = 1;
}
Response.Write ( "\t{0}", myEnumerator.Current );
}
Response.WriteLine ( );
}
/*
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
*/
Array Members Array.CreateInstance Overload List