📜  C#| Type.GetArrayRank()方法

📅  最后修改于: 2021-05-29 21:37:43             🧑  作者: Mango

Type.GetArrayRank()方法用于获取数组中的维数。

下面的程序说明了Type.GetArrayRank()方法的用法:

范例1:

// C# program to demonstrate the
// Type.GetArrayRank() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try 
        {
            // Declaring and intializing Type object
            Type type = typeof(int[,,,,, ]);
  
            // Getting the dimensions
            // using GetArrayRank()
            int rank = type.GetArrayRank();
  
            // Display the rank
            Console.WriteLine("ArrayRank is:  {0}", rank);
        }
  
        catch (ArgumentException e)
        {
            Console.WriteLine("The current type is not an Array");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
ArrayRank is:  6

范例2:

// C# program to demonstrate the
// Type.GetArrayRank() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Declaring and intializing Type object
            Type type = typeof(int);
  
            // Getting ArrayRank by
            // using GetArrayRank()
            int rank = type.GetArrayRank();
  
            // Display the rank
            Console.WriteLine("ArrayRank is :  {0}", rank);
        }
  
        catch (ArgumentException e) 
        {
            Console.WriteLine("The current type is not an Array");
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
The current type is not an Array
Exception Thrown: System.ArgumentException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getarrayrank?view=netcore-3.0