在C#中获取三维数组的上限和下限
要获取上限和下限,请分别使用GetUpperBound()
GetLowerBound()
C#中的方法。
这些方法下要设置的参数是尺寸,即
假设我们的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("Dimension 0 Upper Bound: {0}",arr.GetUpperBound(0).ToString()); Console.WriteLine("Dimension 0 Lower Bound: {0}",arr.GetLowerBound(0).ToString()); Console.WriteLine("Dimension 1 Upper Bound: {0}",arr.GetUpperBound(1).ToString()); Console.WriteLine("Dimension 1 Lower Bound: {0}",arr.GetLowerBound(1).ToString()); Console.WriteLine("Dimension 2 Upper Bound: {0}",arr.GetUpperBound(2).ToString()); Console.WriteLine("Dimension 2 Lower Bound: {0}",arr.GetLowerBound(2).ToString()); } }
输出结果
Dimension 0 Upper Bound: 1 Dimension 0 Lower Bound: 0 Dimension 1 Upper Bound: 2 Dimension 1 Lower Bound: 0 Dimension 2 Upper Bound: 3 Dimension 2 Lower Bound: 0