Get dimension length, c# arrays

前端 未结 5 1629
無奈伤痛
無奈伤痛 2021-01-20 19:21
int[,] arr = new int[2,5];
var rows = arr.?
var cols = arr.?

Assert.Equals(3, rows);
Assert.Equals(6, cols);
5条回答
  •  遥遥无期
    2021-01-20 19:58

    You can use GetLength(some-dimension-starting-from-0) on a array.

    var rows = arr.GetLength(0);
    var cols = arr.GetLength(1);
    

    But rows will be 2 and columns 5.

    var arr = new int[2,3]
    

    Will give you:

    arr[0,0]
    arr[0,1]
    arr[0,2]
    arr[1,0]
    arr[1,1]
    arr[1,2]
    

提交回复
热议问题