Export (0) Print
Expand All

Array.CreateInstance Method

Initializes a new instance of the Array class.

Overload List

Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, Integer) As Array
[C#] public static Array CreateInstance(Type, int);
[C++] public: static Array* CreateInstance(Type*, int);
[JScript] public static function CreateInstance(Type, int) : Array;

Creates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. The dimension lengths are specified in an array of 32-bit integers.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, ParamArray Integer()) As Array
[C#] public static Array CreateInstance(Type, params int[]);
[C++] public: static Array* CreateInstance(Type*, int __gc[]);
[JScript] public static function CreateInstance(Type, int[]) : Array;

Creates a multidimensional Array of the specified Type and dimension lengths, with zero-based indexing. The dimension lengths are specified in an array of 64-bit integers.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, ParamArray Long()) As Array
[C#] public static Array CreateInstance(Type, params long[]);
[C++] public: static Array* CreateInstance(Type*, __int64 __gc[]);
[JScript] public static function CreateInstance(Type, long[]) : Array;

Creates a two-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, Integer, Integer) As Array
[C#] public static Array CreateInstance(Type, int, int);
[C++] public: static Array* CreateInstance(Type*, int, int);
[JScript] public static function CreateInstance(Type, int, int) : Array;

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

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, Integer(), Integer()) As Array
[C#] public static Array CreateInstance(Type, int[], int[]);
[C++] public: static Array* CreateInstance(Type*, int __gc[], int __gc[]);
[JScript] public static function CreateInstance(Type, int[], int[]) : Array;

Creates a three-dimensional Array of the specified Type and dimension lengths, with zero-based indexing.

[Visual Basic] Overloads Public Shared Function CreateInstance(Type, Integer, Integer, Integer) As Array
[C#] public static Array CreateInstance(Type, int, int, int);
[C++] public: static Array* CreateInstance(Type*, int, int, int);
[JScript] public static function CreateInstance(Type, int, int, int) : Array;

Example

The following code example shows how to create and initialize a three-dimensional Array.

[Visual Basic] 
Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a three-dimensional Array of type Object.
        Dim my3DArray As Array = Array.CreateInstance(GetType(Object), 2, 3, 4)
        Dim i As Integer
        For i = my3DArray.GetLowerBound(0) To my3DArray.GetUpperBound(0)
            Dim j As Integer
            For j = my3DArray.GetLowerBound(1) To my3DArray.GetUpperBound(1)
                Dim k As Integer
                For k = my3DArray.GetLowerBound(2) To my3DArray.GetUpperBound(2)
                    my3DArray.SetValue("abc" + i.ToString() _
                       + j.ToString() + k.ToString(), i, j, k)
                Next k 
            Next j
        Next i

        ' Displays the values of the Array.
        Console.WriteLine("The three-dimensional Array contains the " _
           + "following values:")
        PrintValues(my3DArray)
    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
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The three-dimensional Array contains the following values:
'     abc000    abc001    abc002    abc003
'     abc010    abc011    abc012    abc013
'     abc020    abc021    abc022    abc023
'     abc100    abc101    abc102    abc103
'     abc110    abc111    abc112    abc113
'     abc120    abc121    abc122    abc123 

[C#] 
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a three-dimensional Array of type Object.
      Array my3DArray=Array.CreateInstance( typeof(Object), 2, 3, 4 );
      for ( int i = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++ )
         for ( int j = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++ )
            for ( int k = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++ )
               my3DArray.SetValue( "abc" + i + j + k, i, j, k );

      // Displays the values of the Array.
      Console.WriteLine( "The three-dimensional Array contains the following values:" );
      PrintValues( my3DArray );
   }


   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  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "\t{0}", myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
/*
This code produces the following output.

The three-dimensional Array contains the following values:
    abc000    abc001    abc002    abc003
    abc010    abc011    abc012    abc013
    abc020    abc021    abc022    abc023
    abc100    abc101    abc102    abc103
    abc110    abc111    abc112    abc113
    abc120    abc121    abc122    abc123
*/

[C++] 
#using <mscorlib.dll>
using namespace System;

void PrintValues( Array* myArr );

void main()  {
 
    // Creates and initializes a three-dimensional Array instance of type Object.
    Array* my3DArray = Array::CreateInstance( __typeof(Object), 2, 3, 4 );

    for ( int i = my3DArray->GetLowerBound(0); i <= my3DArray->GetUpperBound(0); i++ )
        for ( int j = my3DArray->GetLowerBound(1); j <= my3DArray->GetUpperBound(1); j++ )
            for ( int k = my3DArray->GetLowerBound(2); k <= my3DArray->GetUpperBound(2); k++ )
                my3DArray->SetValue( String::Concat( S"abc", __box(i), __box(j), __box(k)), i, j, k );
 
    // Displays the values of the Array.
    Console::WriteLine( "The three-dimensional Array instance contains the following values:" );
    PrintValues( my3DArray );
}
 
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  {
            Console::WriteLine();
            i = 1;
        }
        Console::Write( "\t{0}", myEnumerator->Current );
    }
    Console::WriteLine();
}

 /*
 This code produces the following output.
 
 The three-dimensional Array instance contains the following values:
     abc000    abc001    abc002    abc003
     abc010    abc011    abc012    abc013
     abc020    abc021    abc022    abc023
     abc100    abc101    abc102    abc103
     abc110    abc111    abc112    abc113
     abc120    abc121    abc122    abc123
 */

[JScript] 
import System;

// Creates and initializes a three-dimensional Array of type Object.
var my3DArray : System.Array = System.Array.CreateInstance( System.Object, 2, 3, 4 );
for ( var i : int = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++ )
  for ( var j : int = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++ )
     for ( var k : int = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++ )
        my3DArray.SetValue( "abc" + i + j + k, i, j, k );

// Displays the values of the Array.
Console.WriteLine( "The three-dimensional Array contains the following values:" );
PrintValues( my3DArray );
  
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  {
         Console.WriteLine();
         i = 1;
      }
      Console.Write( "\t{0}", myEnumerator.Current );
   }
   Console.WriteLine();
}
 /*
 This code produces the following output.
 
 The three-dimensional Array contains the following values:
     abc000    abc001    abc002    abc003
     abc010    abc011    abc012    abc013
     abc020    abc021    abc022    abc023
     abc100    abc101    abc102    abc103
     abc110    abc111    abc112    abc113
     abc120    abc121    abc122    abc123
 */

See Also

Array Class | Array Members | System Namespace

Show:
© 2014 Microsoft