Get dimension length, c# arrays

前端 未结 5 1619
無奈伤痛
無奈伤痛 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 20:02

    You can use GetLength() method of array that let you know what is the length of each dimension.

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

    Just to make clear, it gets the size, so in your example rows will be 2.

提交回复
热议问题