C# Array - GetLength() Method



The C# Array GetLength() method is used to return a 32-bit integer representing the number of elements available in an array of specified dimension.

This method throws an IndexOutOfRangeException if the dimension is zero or if it is greater than or equal to the rank.

Syntax

Following is the syntax of the C# Array GetLength() method −

public int GetLength(int dimension);

Parameters

This method accepts the following parameters −

  • dimension: It represents a zero-based dimension of the array whose length needs to be determined.

Return value

This method returns a 32-bit integer that represents the number of elements in the specified array.

Example 1: Display Length of the Integer Array

This is the basic example of the GetLength() method to display the number of element present in the array −

using System;
class Program {
   static void Main() {
     
      int[] Number = new int[] {1, 2, 3, 4, 5};
   
      // Get the length
      int length = Number.GetLength(0);
   
      Console.WriteLine("Length of the Array: " + length);
   }
}

Output

Following is the output −

Length of the Array: 5

Example 2: Find the Length of 2D Array

Let's create an example that displays the number of rows and columns in the two-dimensional array using theGetLength() method −

using System;
class Program {
   static void Main() {
      int[,] matrix = {
         { 1, 2, 3 },
         { 4, 5, 6 },
         { 7, 8, 9 }
      };

      // Get the number of rows (dimension 0)
      int rows = matrix.GetLength(0);

      // Get the number of columns (dimension 1)
      int columns = matrix.GetLength(1);

      Console.WriteLine($"Rows: {rows}, Columns: {columns}");
   }
}

Output

Following is the output −

Rows: 3, Columns: 3

Example 3: Get the Dimension of 3D-Array

Let's see another example of theGetLength()method to display the number of elements of each dimension in the array −

using System;
class Program {
   static void Main() {
      int[,,] cube = {
         { { 1, 2 }, { 3, 4 } },
         { { 5, 6 }, { 7, 8 } }
      };

      // Get the length of the first dimension
      int dim0 = cube.GetLength(0);

      // Get the length of the second dimension
      int dim1 = cube.GetLength(1);

      // Get the length of the third dimension
      int dim2 = cube.GetLength(2);

      Console.WriteLine($"Dimension 0: {dim0}, Dimension 1: {dim1}, Dimension 2: {dim2}");
   }
}

Output

Following is the output −

Dimension 0: 2, Dimension 1: 2, Dimension 2: 2

Example 4: Dynamic Access to Array Dimensions

Let's see another version of the GetLength() method. In this version, we use the Rank property to determine the number of dimensions.

using System;
class Program {
   static void Main(){
      double[,] table = new double[4, 5];

      for (int i = 0; i < table.Rank; i++){
         Console.WriteLine($"Length of dimension {i}: {table.GetLength(i)}");
      }
   }
}

Output

Following is the output −

Length of dimension 0: 4
Length of dimension 1: 5
csharp_array_class.htm
Advertisements