System Namespace Array Class
Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.
[ VB ]
<Serializable>
Overloads Public Shared Function CreateInstance ( _
ByVal elementType As Type, _
ByVal length As Integer _
) As Array
[ C# ]
[Serializable]
public static Array CreateInstance (
Type elementType,
int length
);
[ C++ ]
[Serializable]
public: static Array* CreateInstance (
Type* elementType,
int length
);
[JScript ]
public Serializable
static function CreateInstance (
elementType : Type,
length : int
) : Array;
- elementType
- The Type of the Array to create.
- length
- The size of the Array to create.
A new one-dimensional Array of the specified Type with the specified length, 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 one-dimensional Array.
[ VB ]
Imports System
Imports Microsoft.VisualBasic
Public Class SamplesArray
Public Shared Sub Main ( )
' Creates and initializes a one-dimensional Array of type Int32.
Dim my1DArray As Array = Array.CreateInstance ( GetType ( Int32 ) , 5 )
Dim i As Integer
For i = my1DArray.GetLowerBound ( 0 ) To my1DArray.GetUpperBound ( 0 )
my1DArray.SetValue ( i + 1, i )
Next i
' Displays the values of the Array.
Response.WriteLine ( "The one-dimensional Array contains the " _
+ "following values:" )
PrintValues ( my1DArray )
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 one-dimensional Array contains the following values:
' 1 2 3 4 5
[ C# ]
using System;
public class SamplesArray {
public static void Main ( ) {
// Creates and initializes a one-dimensional Array of type Int32.
Array my1DArray=Array.CreateInstance ( typeof ( Int32 ) , 5 );
for ( int i = my1DArray.GetLowerBound ( 0 ); i <= my1DArray.GetUpperBound ( 0 ); i++ )
my1DArray.SetValue ( i+1, i );
// Displays the values of the Array.
Response.WriteLine ( "The one-dimensional Array contains the following values:" );
PrintValues ( my1DArray );
}
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 one-dimensional Array contains the following values:
1 2 3 4 5
*/
[JScript ]
import System;
// Creates and initializes a one-dimensional Array of type Int32.
var my1DArray : System.Array =System.Array.CreateInstance ( Int32, 5 );
for ( var i : int = my1DArray.GetLowerBound ( 0 ); i <= my1DArray.GetUpperBound ( 0 ); i++ )
my1DArray.SetValue ( Int32 ( i+1 ) , i );
// Displays the values of the Array.
Response.WriteLine ( "The one-dimensional Array contains the following values:" );
PrintValues ( my1DArray );
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 one-dimensional Array contains the following values:
1 2 3 4 5
*/
Array Members Array.CreateInstance Overload List