System.Array.GetValue 方法 (Int64, Int64)

方法描述

获取二维 Array 中指定位置的值。 索引指定为 64 位整数。

语法定义(C# System.Array.GetValue 方法 (Int64, Int64) 的用法)

[ComVisibleAttribute(false)]
public Object GetValue(
	long index1,
	long index2
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
index1 System-Int64 一个 64 位整数,它表示要获取的 Array 元素的第一维索引。
index2 System-Int64 一个 64 位整数,它表示要获取的 Array 元素的第二维索引。
返回值 System.Object 二维 Array 中指定位置的值。

提示和注释

GetLowerBound 和 GetUpperBound 方法可以确定任何索引是否超出界限。

此方法的运算复杂度是 O(1)。

System.Array.GetValue 方法 (Int64, Int64)例子

下面的代码示例阐释了如何设置和获取一维或多维数组中的特定值。

using System;

public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a one-dimensional array.
      String[] myArr1 = new String[5];

      // Sets the element at index 3.
      myArr1.SetValue( "three", 3 );
      Console.WriteLine( "[3]:   {0}", myArr1.GetValue( 3 ) );


      // Creates and initializes a two-dimensional array.
      String[,] myArr2 = new String[5,5];

      // Sets the element at index 1,3.
      myArr2.SetValue( "one-three", 1, 3 );
      Console.WriteLine( "[1,3]:   {0}", myArr2.GetValue( 1, 3 ) );


      // Creates and initializes a three-dimensional array.
      String[,,] myArr3 = new String[5,5,5];

      // Sets the element at index 1,2,3.
      myArr3.SetValue( "one-two-three", 1, 2, 3 );
      Console.WriteLine( "[1,2,3]:   {0}", myArr3.GetValue( 1, 2, 3 ) );


      // Creates and initializes a seven-dimensional array.
      String[,,,,,,] myArr7 = new String[5,5,5,5,5,5,5];

      // Sets the element at index 1,2,3,0,1,2,3.
      int[] myIndices = new int[7] { 1, 2, 3, 0, 1, 2, 3 };
      myArr7.SetValue( "one-two-three-zero-one-two-three", myIndices );
      Console.WriteLine( "[1,2,3,0,1,2,3]:   {0}", myArr7.GetValue( myIndices ) );

   }

}


/* 
This code produces the following output.

[3]:   three
[1,3]:   one-three
[1,2,3]:   one-two-three
[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three

*/

异常

异常 异常描述
ArgumentException 当前的 Array 不是正好有两维。
ArgumentOutOfRangeException index1 或 index2 超出当前 Array 的对应维度的有效索引的范围。

命名空间

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 系统要求。