📜  C#中数组的指定维中的元素总数

📅  最后修改于: 2021-05-30 01:49:24             🧑  作者: Mango

Array.GetLongLength(Int32)方法用于获取一个64位整数,该整数表示Array指定维中的元素数。

句法:

public long GetLongLength (int dimension);

这里,维度是要计算其长度的数组的从零开始的维度。

返回值:返回一个64-bit整数,该整数表示指定维中的元素数。

异常:如果维度小于零或大于或等于Rank,则此方法将引发IndexOutOfRangeException

例子:

// C# program to illustrate the
// Array.GetLongLength() method
using System;
  
namespace geeksforgeeks {
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Three-dimensional array.
        int[,, ] arr = new int[,, ] {
                                      { { 1, 2, 3 },
                                        { 4, 5, 6 },
                                        { 6, 7, 8 } 
                                            
                                      },
                                          
                                      { { 11, 12, 13 },
                                        { 14, 15, 16 },
                                        { 17, 18, 19 } 
                                      },
                                        
                                      { { 21, 22, 23 },
                                        { 24, 25, 26 },
                                        { 27, 28, 29 } 
                                      },
                                    };
  
        Console.Write("Total Number of Elements in"
                    + " first dimension of arr: ");
  
        // using GetLongLength Method
        Console.Write(arr.GetLongLength(0));
  
        // getting the type of returned value
        Console.WriteLine("\nType of returned Length: "
                        + (arr.GetLongLength(0)).GetType());
  
        // showing difference between GetLength
        // and GetLongLength method by getting
        // the type of the both method's
        // returned value
  
        Console.Write("\nTotal Number of Elements in "
                    + "second dimension of arr: ");
  
        // using GetLength Method
        Console.Write(arr.GetLength(1));
  
        // getting the type of returned value
        Console.WriteLine("\nType of returned Length: "
                        + (arr.GetLength(1)).GetType());
  
        Console.Write("\nTotal Number of Elements in "
                    + "second dimension of arr: ");
  
        // using GetLongLength() Method
        Console.Write(arr.GetLongLength(1));
  
        // getting the type of returned value
        Console.WriteLine("\nType of returned Length: "
                        + (arr.GetLongLength(1)).GetType());
    }
}
}
输出:
Total Number of Elements in first dimension of arr: 3
Type of returned Length: System.Int64

Total Number of Elements in second dimension of arr: 3
Type of returned Length: System.Int32

Total Number of Elements in second dimension of arr: 3
Type of returned Length: System.Int64

注意:在上述程序中, GetLength方法的返回类型为System.Int32GetLongLength方法的返回类型为System.Int64

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.array.getlonglength?view=netframework-4.7.2