當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Type.GetArrayRank()用法及代碼示例


Type.GetArrayRank()方法用於獲取數組中的維數。

用法: public virtual int GetArrayRank ();

返回值:此方法返回一個整數,其中包含當前類型的維數。


異常:如果當前類型不是數組,則此方法將引發ArgumentException。

以下示例程序旨在說明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

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetArrayRank() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。