C# | Total number of elements present in an array

Array.GetLength(Int32) Method is used to find the total number of elements present in the specified dimension of the Array.

Syntax:

public int GetLength (int dimension);

Here, dimension is a zero-based dimension of the Array whose length needs to be determined.

Return value: The return type of this method is System.Int32. This method return a 32-bit integer that represents the number of elements in the specified dimension.

Exception:This method will give IndexOutOfRangeException if the value of dimension is less than zero or if the value of dimension is equal to or greater than Rank.

Below given are some examples to understand the implementation in a better way:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// use of GetLength() method
using System;
  
public class GFG {
      
    // Main method
    static public void Main()
    {
  
        // create and initalize array
        int[] myarray = {445, 44, 66, 6666667, 78, 878, 1};
  
        // Display the array
        Console.WriteLine("The elements of myarray :");
          
        foreach(int i in myarray)
        {
            Console.WriteLine(i);
        }
  
        // Find the number of element in myarray
        int result = myarray.GetLength(0);
        Console.WriteLine("Total Elements: {0}", result);
    }
}

chevron_right


Output:

The elements of myarray :
445
44
66
6666667
78
878
1
Total Elements: 7

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check arrays contain
// same number of elements or not
using System;
  
public class GFG {
      
    // Main method
    static public void Main()
    {
  
        // create and initalizing array
        int[] myarray1 = {100, 0, 400, 660, 700, 809, 0};
        int[] myarray2 = {100, 0, 400, 660, 700};
        int[] myarray3 = {100, 0, 400, 660, 700, 809, 0};
  
        // Find the number of element in myarray
        // using GetLength() method
        int result1 = myarray1.GetLength(0);
        int result2 = myarray2.GetLength(0);
        int result3 = myarray3.GetLength(0);
  
        // Check if myarray1, myarray2, myarray3 
        // contain the same number of elements or not
        Console.WriteLine("myarray1 and myarray2: {0}"
                             Equals(result1, result2));
                               
        Console.WriteLine("myarray1 and myarray3: {0}",
                             Equals(result1, result3));
    }
}

chevron_right


Output:

myarray1 and myarray2: False
myarray1 and myarray3: True

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.array.getlength?view=netcore-2.1



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.