Total number of elements in a specified dimension of an Array in C#

Array.GetLongLength(Int32) Method is used to get a 64-bit integer that represents the number of elements in the specified dimension of the Array.

Syntax:

public long GetLongLength (int dimension);

Here, dimension is the zero-based dimension of the Array whose length is to be calculated.



Return Value: It returns a 64-bit integer that represents the number of elements in the specified dimension.

Exception: This method throws IndexOutOfRangeException if the dimension is less than zero or greater than or equal to Rank.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}
}

chevron_right


Output:

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

Note: In the above program, GetLength method return type is System.Int32 but the GetLongLength method return type is System.Int64.

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.