在 C# 中获取三维数组的 Upperbound 和 Lowerbound

csharpprogrammingserver side programming

要获取 Upperbound 和 Lowerbound,请分别使用 C# 中的 GetUpperBound() GetLowerBound() 方法。

这些方法下要设置的参数是维度,即

假设我们的 3D 数组是 −

int[,,] arr = new int[2,3,4];

对于三维数组,维度为 0。

arr.GetUpperBound(0)
arr.GetLowerBound(0)

对于三维数组,维度为 1。

arr.GetUpperBound(1)
arr.GetLowerBound(1)

对于三维数组,维度为 2。

arr.GetUpperBound(2)
arr.GetLowerBound(2)

示例

using System;
class Program {
   static void Main() {
      int[,,] arr = new int[2,3,4];
      Console.WriteLine("维度 0 上界:{0}",arr.GetUpperBound(0).ToString());
      Console.WriteLine("维度 0 下界:{0}",arr.GetLowerBound(0).ToString());
      Console.WriteLine("维度 1 上界:{0}",arr.GetUpperBound(1).ToString());
      Console.WriteLine("维度 1 下界:{0}",arr.GetLowerBound(1).ToString());
      Console.WriteLine("维度 2 上界:{0}",arr.GetUpperBound(2).ToString());
      Console.WriteLine("维度 2 下界:{0}",arr.GetLowerBound(2).ToString());
   }
}

输出

维度 0 上界:1
维度 0 下界:0
维度 1 上界:2
维度 1 下界:0
维度 2 上界:3
维度 2 下界:0

相关文章