Array.CreateInstance 方法

定义

初始化 Array 类的新实例。

重载

CreateInstance(Type, Int32)

创建使用从零开始的索引、具有指定 Type 和长度的一维 Array

CreateInstance(Type, Int32[])

创建索引从零开始、具有指定 Type 和维长的多维 Array。 维的长度在一个 32 位整数数组中指定。

CreateInstance(Type, Int64[])

创建索引从零开始、具有指定 Type 和维长的多维 Array。 维度的长度在一个 64 位整数数组中指定。

CreateInstance(Type, Int32, Int32)

创建使用从零开始的索引、具有指定 Type 和维长的二维 Array

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

创建具有指定下限、指定 Type 和维长的多维 Array

CreateInstance(Type, Int32, Int32, Int32)

创建使用从零开始的索引、具有指定 Type 和维长的三维 Array

CreateInstance(Type, Int32)

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

创建使用从零开始的索引、具有指定 Type 和长度的一维 Array

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

参数

elementType
Type

要创建的 ArrayType

length
Int32

要创建的 Array 的大小。

返回

使用从零开始的索引、具有指定 Type 和长度的新的一维 Array

例外

elementTypenull

elementType 不是有效的 Type

不支持 elementType。 例如,不支持 Void

- 或 -

elementType 为开放式泛型类型。

length 小于零。

示例

下面的代码示例演示如何创建和初始化一维 Array

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

注解

与大多数类不同, Array 提供 CreateInstance 方法(而不是公共构造函数)以允许后期绑定访问。

引用类型元素初始化为 null。 值类型元素初始化为零。

此方法是 O (n) 操作,其中 nlength

在 F# 中,通常改用 Array.zeroCreate 函数。

适用于

.NET 9 和其他版本
产品 版本
.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

创建索引从零开始、具有指定 Type 和维长的多维 Array。 维的长度在一个 32 位整数数组中指定。

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

参数

elementType
Type

要创建的 ArrayType

lengths
Int32[]

一个 32 位整数数组,它表示要创建的 Array 中每个维度的大小。

返回

指定 Type 的新多维 Array,该数组每个维具有指定长度,且使用从零开始的索引。

例外

elementTypenull

lengthsnull

elementType 不是有效的 Type

- 或 -

lengths 数组包含的元素少于一个。

不支持 elementType。 例如,不支持 Void

- 或 -

elementType 为开放式泛型类型。

lengths 中的任何值都小于零。

示例

下面的代码示例演示如何创建和初始化多维 Array

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

注解

与大多数类不同, Array 提供 CreateInstance 方法(而不是公共构造函数)以允许后期绑定访问。

数组中的 lengths 元素数必须等于新 Array中的维度数。 数组的每个元素 lengths 都必须在新 中指定相应维度的 Array长度。

引用类型元素初始化为 null。 值类型元素初始化为零。

此方法是 O (n) 操作,其中 n 是 中 lengths所有值的乘积。

适用于

.NET 9 和其他版本
产品 版本
.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

创建索引从零开始、具有指定 Type 和维长的多维 Array。 维度的长度在一个 64 位整数数组中指定。

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

参数

elementType
Type

要创建的 ArrayType

lengths
Int64[]

一个 64 位整数数组,它表示要创建的 Array 中每个维度的大小。 数组中的每个整数都必须介于零和 Int32.MaxValue 之间(含)。

返回

指定 Type 的新多维 Array,该数组每个维具有指定长度,且使用从零开始的索引。

例外

elementTypenull

lengthsnull

elementType 不是有效的 Type

- 或 -

lengths 数组包含的元素少于一个。

不支持 elementType。 例如,不支持 Void

- 或 -

elementType 为开放式泛型类型。

中的任何 lengths 值都小于零或大于 Int32.MaxValue

示例

下面的代码示例演示如何创建和初始化多维 Array

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

注解

与大多数类不同, Array 提供 CreateInstance 方法(而不是公共构造函数)以允许后期绑定访问。

数组中的 lengths 元素数必须等于新 Array中的维度数。 数组的每个元素 lengths 都必须在新 中指定相应维度的 Array长度。

引用类型元素初始化为 null。 值类型元素初始化为零。

此方法是 O (n) 操作,其中 n 是 中 lengths所有值的乘积。

适用于

.NET 9 和其他版本
产品 版本
.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

创建使用从零开始的索引、具有指定 Type 和维长的二维 Array

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

参数

elementType
Type

要创建的 ArrayType

length1
Int32

要创建的 Array 的第一维的大小。

length2
Int32

要创建的 Array 的第二维的大小。

返回

使用从零开始的索引、具有指定 Type 的新的二维 Array,其每个维度都为指定的长度。

例外

elementTypenull

elementType 不是有效的 Type

不支持 elementType。 例如,不支持 Void

- 或 -

elementType 为开放式泛型类型。

length1 小于零。

length2 小于零。

示例

下面的代码示例演示如何创建和初始化二维 Array

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

注解

与大多数类不同, Array 提供 CreateInstance 方法(而不是公共构造函数)以允许后期绑定访问。

引用类型元素初始化为 null。 值类型元素初始化为零。

此方法是 O (n) 操作,其中 n 是 和 length2length1乘积。

在 F# 中,可以改用 Array2D.zeroCreate 函数。

适用于

.NET 9 和其他版本
产品 版本
.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

创建具有指定下限、指定 Type 和维长的多维 Array

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

参数

elementType
Type

要创建的 ArrayType

lengths
Int32[]

一维数组,它包含要创建的 Array 的每个维度的大小。

lowerBounds
Int32[]

一维数组,它包含要创建的 Array 的每个维度的下限(起始索引)。

返回

具有指定 Type 的新的多维 Array,其每个维度都具有指定长度和下限。

例外

elementTypenull

lengths 上声明的默认值为 null

lowerBoundsnull

elementType 不是有效的 Type

- 或 -

lengths 数组包含的元素少于一个。

- 或 -

lengthslowerBounds 数组包含的元素数不同。

不支持 elementType。 例如,不支持 Void

- 或 -

elementType 为开放式泛型类型。

lengths 中的任何值都小于零。

- 或 -

中的任何 lowerBounds 值都非常大,因此维度的下限和长度之和大于 Int32.MaxValue

示例

下面的代码示例演示如何创建和初始化具有指定下限的多维 Array

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

注解

与大多数类不同, Array 提供 CreateInstance 方法(而不是公共构造函数)以允许后期绑定访问。

lengthslowerBounds 数组必须具有相同数量的元素。 数组中的 lengths 元素数必须等于新 Array中的维度数。

数组的每个元素 lengths 都必须在新 中指定相应维度的 Array长度。

数组的每个元素 lowerBounds 都必须在新 中指定相应维度的 Array下限。 通常,.NET 类库和许多编程语言不会处理非零下限。

引用类型元素初始化为 null。 值类型元素初始化为零。

此方法是 O (n) 操作,其中 n 是 中 lengths所有值的乘积。

备注

并非所有语言都支持具有非零下限的数组,因此可能无法将基于 Array 非零的实例强制转换为语言的数组类型。 例如,不能将下限为 6 的一维整数数组强制转换为 C# int[] 的类型。 这会导致 InvalidCastException 在运行时出现消息“无法将类型'System.Int32[*]'的对象强制转换为类型'System.Int32[]”。,其中星号 (*) 表示非从零开始的索引。 但是,可以将使用 CreateInstance(Type, Int32[], Int32[]) 创建的任何排名的从零开始的数组强制转换为语言的 数组。 例如,可以将使用此方法创建的基于 2 维零的 int[,] 整数数组强制转换为 C# 的类型。

适用于

.NET 9 和其他版本
产品 版本
.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

创建使用从零开始的索引、具有指定 Type 和维长的三维 Array

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

参数

elementType
Type

要创建的 ArrayType

length1
Int32

要创建的 Array 的第一维的大小。

length2
Int32

要创建的 Array 的第二维的大小。

length3
Int32

要创建的 Array 的第三维的大小。

返回

每个维具有指定长度、使用从零开始的索引的指定 Type 的新三维 Array

例外

elementTypenull

elementType 不是有效的 Type

不支持 elementType。 例如,不支持 Void

- 或 -

elementType 为开放式泛型类型。

length1 小于零。

length2 小于零。

length3 小于零。

示例

下面的代码示例演示如何创建和初始化三维 Array

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

注解

与大多数类不同, Array 提供 CreateInstance 方法(而不是公共构造函数)以允许后期绑定访问。

引用类型元素初始化为 null。 值类型元素初始化为零。

此方法是 O (n) 操作,其中 n 是 、 length2length3length1乘积。

在 F# 中,可以改用 Array3D.zeroCreate 函数。

适用于

.NET 9 和其他版本
产品 版本
.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