C# Array CreateInstance(Type, Int32[], Int32[]) Array

Description

Array CreateInstance(Type, Int32[], Int32[]) creates a multidimensional Array of the specified Type and dimension lengths, with the specified lower bounds.

Syntax

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


public static Array CreateInstance(
  Type elementType,//from   ww w. j  ava2s.  c  o  m
  int[] lengths,
  int[] lowerBounds
)

Parameters

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

  • 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.

Returns

Array.CreateInstance(Type, Int32[], Int32[]) method returns A new multidimensional Array of the specified Type with the specified length and lower bound for each dimension.

Example

The following code example shows how to create and initialize a multidimensional Array with specified lower bounds.


using System;/*from   w  w  w.  j av a  2 s  . c  o  m*/
public class SamplesArray  {

   public static void Main()  {

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

      for ( int i = 0; i < myArray.Rank; i++ )
         Console.WriteLine( "{0}:\t{1}\t{2}", i, myArray.GetLowerBound(i), myArray.GetUpperBound(i) );

   }
}

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