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

方法描述

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

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

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

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
elementType System-Type 要创建的 Array 的 Type。
lengths System-Int32[] 一维数组,它包含要创建的 Array 的每个维度的大小。
lowerBounds System-Int32[] 一维数组,它包含要创建的 Array 的每个维度的下限(起始索引)。
返回值 System.Array 新的指定 Type 的多维 Array,每个维度都有指定的长度和下限。

提示和注释

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

lengths 和 lowerBounds 数组必须具有相同的元素数。 lengths 数组中的元素数必须等于新 Array 中的维数。

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

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

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

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

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

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

using System;
public class SamplesArray  {

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

异常

异常 异常描述
ArgumentNullException
  • elementType 为 null。
  • lengths 为 null。
  • lowerBounds 为 null。
ArgumentException
  • elementType 不是有效的 Type。
  • lengths 数组包含的元素少于一个。
  • lengths 和 lowerBounds 数组包含的元素数不同。
NotSupportedException
  • 不支持 elementType。 例如,不支持 Void。
  • elementType 是一个开放式泛型类型。
ArgumentOutOfRangeException
  • lengths 中的任何值都小于零。
  • lowerBounds 中的任意一个值都很大,因此,维的下限和长度的和大于 Int32.MaxValue。

命名空间

namespace: System

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

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .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 系统要求。