C# Array CreateInstance(Type, Int64[])

Description

Array CreateInstance(Type, Int64[]) 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.

Syntax

Array.CreateInstance(Type, Int64[]) has the following syntax.


public static Array CreateInstance(
  Type elementType,
  params long[] lengths
)

Parameters

Array.CreateInstance(Type, Int64[]) has the following parameters.

  • elementType - The Type of the Array to create.
  • lengths - An array of 64-bit integers that represent the size of each dimension of the Array to create. Each integer in the array must be between zero and Int32.MaxValue, inclusive.

Returns

Array.CreateInstance(Type, Int64[]) method returns A new multidimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

Example

The following code example shows how to create and initialize a multidimensional Array.


/* w  ww  .  j  a  va2s .  co m*/
using System;
public class SamplesArray  {

   public static void Main()  {

      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( i, myIndicesArray );
               }

   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version