Edit

Share via


Array.CreateInstance Method

Definition

Initializes a new instance of the Array class.

Overloads

CreateInstance(Type, Int32)

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

CreateInstance(Type, Int32[])

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.

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.

CreateInstance(Type, Int32, Int32)

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

CreateInstance(Type, Int32[], Int32[])

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

CreateInstance(Type, Int32, Int32, Int32)

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

CreateInstance(Type, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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

public static Array CreateInstance (Type elementType, int length);

Parameters

elementType
Type

The Type of the Array to create.

length
Int32

The size of the Array to create.

Returns

A new one-dimensional Array of the specified Type with the specified length, using zero-based indexing.

Exceptions

elementType is null.

elementType is not a valid Type.

elementType is not supported. For example, Void is not supported.

-or-

elementType is an open generic type.

length is less than zero.

Examples

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

using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a one-dimensional Array of type int.
      Array my1DArray=Array.CreateInstance( typeof(int), 5 );
      for ( int i = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0); i++ )
         my1DArray.SetValue( i+1, i );

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

The one-dimensional Array contains the following values:
    1    2    3    4    5
*/

Remarks

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

Reference-type elements are initialized to null. Value-type elements are initialized to zero.

This method is an O(n) operation, where n is length.

In F#, the Array.zeroCreate function is usually used instead.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

CreateInstance(Type, Int32[])

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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.

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

Parameters

elementType
Type

The Type of the Array to create.

lengths
Int32[]

An array of 32-bit integers that represent the size of each dimension of the Array to create.

Returns

A new multidimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

Exceptions

elementType is null.

-or-

lengths is null.

elementType is not a valid Type.

-or-

The lengths array contains less than one element.

elementType is not supported. For example, Void is not supported.

-or-

elementType is an open generic type.

Any value in lengths is less than zero.

Examples

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

using System;
public class SamplesArray3  {

   public static void Main()  {

      // Creates and initializes a multidimensional Array of type string.
      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( Convert.ToString(i) + j + k + l, myIndicesArray );
               }

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

   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 four-dimensional Array contains the following values:
    0000    0001    0002    0003    0004
    0010    0011    0012    0013    0014
    0020    0021    0022    0023    0024
    0030    0031    0032    0033    0034
    0100    0101    0102    0103    0104
    0110    0111    0112    0113    0114
    0120    0121    0122    0123    0124
    0130    0131    0132    0133    0134
    0200    0201    0202    0203    0204
    0210    0211    0212    0213    0214
    0220    0221    0222    0223    0224
    0230    0231    0232    0233    0234
    1000    1001    1002    1003    1004
    1010    1011    1012    1013    1014
    1020    1021    1022    1023    1024
    1030    1031    1032    1033    1034
    1100    1101    1102    1103    1104
    1110    1111    1112    1113    1114
    1120    1121    1122    1123    1124
    1130    1131    1132    1133    1134
    1200    1201    1202    1203    1204
    1210    1211    1212    1213    1214
    1220    1221    1222    1223    1224
    1230    1231    1232    1233    1234
*/

Remarks

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

The number of elements in the lengths array must equal the number of dimensions in the new Array. Each element of the lengths array must specify the length of the corresponding dimension in the new Array.

Reference-type elements are initialized to null. Value-type elements are initialized to zero.

This method is an O(n) operation, where n is the product of all values in lengths.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

CreateInstance(Type, Int64[])

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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.

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

Parameters

elementType
Type

The Type of the Array to create.

lengths
Int64[]

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

A new multidimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

Exceptions

elementType is null.

-or-

lengths is null.

elementType is not a valid Type.

-or-

The lengths array contains less than one element.

elementType is not supported. For example, Void is not supported.

-or-

elementType is an open generic type.

Any value in lengths is less than zero or greater than Int32.MaxValue.

Examples

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

using System;
public class SamplesArray3  {

   public static void Main()  {

      // Creates and initializes a multidimensional Array of type string.
      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( Convert.ToString(i) + j + k + l, myIndicesArray );
               }

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

   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 four-dimensional Array contains the following values:
    0000    0001    0002    0003    0004
    0010    0011    0012    0013    0014
    0020    0021    0022    0023    0024
    0030    0031    0032    0033    0034
    0100    0101    0102    0103    0104
    0110    0111    0112    0113    0114
    0120    0121    0122    0123    0124
    0130    0131    0132    0133    0134
    0200    0201    0202    0203    0204
    0210    0211    0212    0213    0214
    0220    0221    0222    0223    0224
    0230    0231    0232    0233    0234
    1000    1001    1002    1003    1004
    1010    1011    1012    1013    1014
    1020    1021    1022    1023    1024
    1030    1031    1032    1033    1034
    1100    1101    1102    1103    1104
    1110    1111    1112    1113    1114
    1120    1121    1122    1123    1124
    1130    1131    1132    1133    1134
    1200    1201    1202    1203    1204
    1210    1211    1212    1213    1214
    1220    1221    1222    1223    1224
    1230    1231    1232    1233    1234
*/

Remarks

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

The number of elements in the lengths array must equal the number of dimensions in the new Array. Each element of the lengths array must specify the length of the corresponding dimension in the new Array.

Reference-type elements are initialized to null. Value-type elements are initialized to zero.

This method is an O(n) operation, where n is the product of all values in lengths.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

CreateInstance(Type, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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

public static Array CreateInstance (Type elementType, int length1, int length2);

Parameters

elementType
Type

The Type of the Array to create.

length1
Int32

The size of the first dimension of the Array to create.

length2
Int32

The size of the second dimension of the Array to create.

Returns

A new two-dimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

Exceptions

elementType is null.

elementType is not a valid Type.

elementType is not supported. For example, Void is not supported.

-or-

elementType is an open generic type.

length1 is less than zero.

-or-

length2 is less than zero.

Examples

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

using System;
public class SamplesArray1  {

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

The two-dimensional Array contains the following values:
    abc00    abc01    abc02
    abc10    abc11    abc12
*/

Remarks

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

Reference-type elements are initialized to null. Value-type elements are initialized to zero.

This method is an O(n) operation, where n is the product of length1 and length2.

In F#, the Array2D.zeroCreate function can be used instead.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

CreateInstance(Type, Int32[], Int32[])

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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

public static Array CreateInstance (Type elementType, int[] lengths, int[] lowerBounds);

Parameters

elementType
Type

The Type of the Array to create.

lengths
Int32[]

A one-dimensional array that contains the size of each dimension of the Array to create.

lowerBounds
Int32[]

A one-dimensional array that contains the lower bound (starting index) of each dimension of the Array to create.

Returns

A new multidimensional Array of the specified Type with the specified length and lower bound for each dimension.

Exceptions

elementType is null.

-or-

lengths is null.

-or-

lowerBounds is null.

elementType is not a valid Type.

-or-

The lengths array contains less than one element.

-or-

The lengths and lowerBounds arrays do not contain the same number of elements.

elementType is not supported. For example, Void is not supported.

-or-

elementType is an open generic type.

Any value in lengths is less than zero.

-or-

Any value in lowerBounds is very large, such that the sum of a dimension's lower bound and length is greater than Int32.MaxValue.

Examples

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

using System;
public class SamplesArray4  {

   public static void Main()  {

      // Creates and initializes a multidimensional Array of type string.
      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( Convert.ToString(i) + j, myIndicesArray );
         }

      // Displays the lower bounds and the upper bounds of each dimension.
      Console.WriteLine( "Bounds:\tLower\tUpper" );
      for ( int i = 0; i < myArray.Rank; i++ )
         Console.WriteLine( "{0}:\t{1}\t{2}", i, myArray.GetLowerBound(i), myArray.GetUpperBound(i) );

      // Displays the values of the Array.
      Console.WriteLine( "The Array contains the following values:" );
      PrintValues( myArray );
   }

   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.

Bounds:    Lower    Upper
0:    2    4
1:    3    7
The Array contains the following values:
    23    24    25    26    27
    33    34    35    36    37
    43    44    45    46    47
*/

Remarks

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

The lengths and lowerBounds arrays must have the same number of elements. The number of elements in the lengths array must equal the number of dimensions in the new Array.

Each element of the lengths array must specify the length of the corresponding dimension in the new Array.

Each element of the lowerBounds array must specify the lower bound of the corresponding dimension in the new Array. Generally, the .NET class library and many programming languages do not handle non-zero lower bounds.

Reference-type elements are initialized to null. Value-type elements are initialized to zero.

This method is an O(n) operation, where n is the product of all values in lengths.

Note

Not all languages support arrays with non-zero lower bounds, and therefore you may not be able to cast instances of non-zero based Array to the language's array type. For example, you cannot cast a 1-dimensional integer array with the lower bound 6 to C#'s int[] type. This results in InvalidCastException during runtime with the message "Unable to cast object of type 'System.Int32[*]' to type 'System.Int32[]'.", where the asterisk (*) means non-zero based index. However, you can cast zero based arrays of any rank created with CreateInstance(Type, Int32[], Int32[]) to the language's array. For example, you can cast a 2-dimensional zero based integer array created with this method to C#'s int[,] type.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

CreateInstance(Type, Int32, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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

public static Array CreateInstance (Type elementType, int length1, int length2, int length3);

Parameters

elementType
Type

The Type of the Array to create.

length1
Int32

The size of the first dimension of the Array to create.

length2
Int32

The size of the second dimension of the Array to create.

length3
Int32

The size of the third dimension of the Array to create.

Returns

A new three-dimensional Array of the specified Type with the specified length for each dimension, using zero-based indexing.

Exceptions

elementType is null.

elementType is not a valid Type.

elementType is not supported. For example, Void is not supported.

-or-

elementType is an open generic type.

length1 is less than zero.

-or-

length2 is less than zero.

-or-

length3 is less than zero.

Examples

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

using System;
public class SamplesArray2  {

   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
*/

Remarks

Unlike most classes, Array provides the CreateInstance method, instead of public constructors, to allow for late bound access.

Reference-type elements are initialized to null. Value-type elements are initialized to zero.

This method is an O(n) operation, where n is the product of length1, length2, and length3.

In F#, the Array3D.zeroCreate function can be used instead.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1