Ví dụ tìm số chiều của mảng dùng thuộc tính Rank.
arr.Rank
Ở đây, arr là mảng:
int[,] arr = new int[5,5];
Muốn lấy hàng và cột, dùng GetLength:
arr.GetLength(0);
arr.GetLength(1);
Code hoàn chỉnh:
Ví dụ
using System;
class Program {
static void Main() {
int[,] arr = new int[4,5];
Console.WriteLine(arr.GetLength(0));
Console.WriteLine(arr.GetLength(1));
Console.WriteLine("Upper Bound: {0}", arr.GetUpperBound(0).ToString());
Console.WriteLine("Lower Bound: {0}", arr.GetLowerBound(0).ToString());
Console.WriteLine("Dimensions of Array : " + arr.Rank);
}
}
Kết quả
4
5
Upper Bound: 3
Lower Bound: 0
Dimensions of Array : 2