System.Array.CreateInstance 方法 (Type, Int64[])

方法描述

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

语法定义(C# System.Array.CreateInstance 方法 (Type, Int64[]) 的用法)

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

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
elementType System-Type 要创建的 Array 的 Type。
lengths System-Int64[] 一个 64 位整数数组,它表示要创建的 Array 中每个维的大小。数组中的每个整数都必须介于零和 Int32.MaxValue 之间(均含)。
返回值 System.Array 使用从零开始的索引、具有指定 Type 的新的多维 Array,其每个维度都为指定的长度。

提示和注释

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

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

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

此方法的运算复杂度为 O(n),其中 n 是 lengths 中所有值的乘积。

System.Array.CreateInstance 方法 (Type, Int64[])例子

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

using System;
public class SamplesArray  {

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

异常

异常 异常描述
ArgumentNullException
  • elementType 为 null。
  • lengths 为 null。
ArgumentException
  • elementType 不是有效的 Type。
  • lengths 数组包含的元素少于一个。
NotSupportedException
  • 不支持 elementType。 例如,不支持 Void。
  • elementType 是一个开放式泛型类型。
ArgumentOutOfRangeException lengths 中的所有值都小于零或大于 Int32.MaxValue。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。