System Namespace Array Class
Creates a two-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.
[ VB ]
<Serializable>
Overloads Public Shared Function CreateInstance ( _
ByVal elementType As Type, _
ByVal length1 As Integer, _
ByVal length2 As Integer _
) As Array
[ C# ]
[Serializable]
public static Array CreateInstance (
Type elementType,
int length1,
int length2
);
[ C++ ]
[Serializable]
public: static Array* CreateInstance (
Type* elementType,
int length1,
int length2
);
[JScript ]
public Serializable
static function CreateInstance (
elementType : Type,
length1 : int,
length2 : int
) : Array;
- 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.
A new two-dimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.
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.
The following code example shows how to create and initialize a two-dimensional Array.
[ VB ]
Imports System
Imports Microsoft.VisualBasic
Public Class SamplesArray
Public Shared Sub Main ( )
' Creates and initializes a two-dimensional Array of type String.
Dim my2DArray As Array = Array.CreateInstance ( GetType ( String ) , 2, 3 )
Dim i, j As Integer
For i = my2DArray.GetLowerBound ( 0 ) To my2DArray.GetUpperBound ( 0 )
For j = my2DArray.GetLowerBound ( 1 ) To my2DArray.GetUpperBound ( 1 )
my2DArray.SetValue ( "abc" + i.ToString ( ) + j.ToString ( ) , i, j )
Next j
Next i
' Displays the values of the Array.
Response.WriteLine ( "The two-dimensional Array contains the " _
+ "following values:" )
PrintValues ( my2DArray )
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.
'
' The two-dimensional Array contains the following values:
' abc00 abc01 abc02
' abc10 abc11 abc12
[ C# ]
using System;
public class SamplesArray {
public static void Main ( ) {
// Creates and initializes a two-dimensional Array of type String.
Array my2DArray=Array.CreateInstance ( typeof ( String ) , 2, 3 );
for ( int i = my2DArray.GetLowerBound ( 0 ); i <= my2DArray.GetUpperBound ( 0 ); i++ )
for ( int j = my2DArray.GetLowerBound ( 1 ); j <= my2DArray.GetUpperBound ( 1 ); j++ )
my2DArray.SetValue ( "abc" + i + j, i, j );
// Displays the values of the Array.
Response.WriteLine ( "The two-dimensional Array contains the following values:" );
PrintValues ( my2DArray );
}
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.
The two-dimensional Array contains the following values:
abc00 abc01 abc02
abc10 abc11 abc12
*/
[JScript ]
import System;
// Creates and initializes a two-dimensional Array of type String.
var my2DArray : System.Array = System.Array.CreateInstance ( System.String, 2, 3 );
for ( var i : int = my2DArray.GetLowerBound ( 0 ); i <= my2DArray.GetUpperBound ( 0 ); i++ )
for ( var j : int = my2DArray.GetLowerBound ( 1 ); j <= my2DArray.GetUpperBound ( 1 ); j++ )
my2DArray.SetValue ( "abc" + i + j, i, j );
// Displays the values of the Array.
Response.WriteLine ( "The two-dimensional Array contains the following values:" );
PrintValues ( my2DArray );
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.
The two-dimensional Array contains the following values:
abc00 abc01 abc02
abc10 abc11 abc12
*/
Array Members Array.CreateInstance Overload List