Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Type.GetArrayRank() trong C#

Phương thức Type.GetArrayRank() trong C# lấy số chiều (dimensions) của một mảng.

Cú pháp

public virtual int GetArrayRank();

Ví dụ 1: Mảng 3 chiều

using System;
public class Demo {
    public static void Main(string[] args) {
        Type type = typeof(int[,,]);
        int arrRank = type.GetArrayRank();
        Console.WriteLine("Array Rank = {0}", arrRank);
    }
}

Kết quả

Array Rank = 3

Ví dụ 2: Mảng 8 chiều

using System;
public class Demo {
    public static void Main(string[] args) {
        Type type = typeof(string[,,,,,,,]);
        Type type2 = typeof(string[,,,,,,,]);
        int arrRank = type.GetArrayRank();
        Console.WriteLine("Array Rank = {0}", arrRank);
        Console.WriteLine("Hai kiểu có bằng nhau? {0}", type.Equals(type2));
    }
}

Kết quả

Array Rank = 8
Hai kiểu có bằng nhau? True