How to find the length and rank of a jagged array in C#?

A jagged array in C# is an array of arrays where each sub-array can have different lengths. Unlike multi-dimensional arrays, jagged arrays provide flexibility in storing varying amounts of data in each row.

To find the length and rank of a jagged array, you can use the Length property, GetLowerBound() and GetUpperBound() methods, and the Rank property.

Syntax

Following is the syntax for declaring a jagged array −

dataType[][] arrayName = new dataType[rows][];

Following are the methods and properties to find length and rank −

arrayName.Length           // Number of rows (sub-arrays)
arrayName[i].Length        // Length of specific row
arrayName.GetLowerBound(0) // Lower bound of first dimension
arrayName.GetUpperBound(0) // Upper bound of first dimension
arrayName.Rank             // Number of dimensions

Jagged Array Structure arr[0] arr[1] arr[2] arr[3] 0 0 1 2 2 4 3 6 Length = 2 Length = 2 Length = 2 Length = 2 arr.Length = 4 (rows)

Finding Length and Rank of Jagged Array

Example

using System;

public class Program {
   public static void Main() {
      int[][] arr = new int[][] {
         new int[] { 0, 0 },
         new int[] { 1, 2 },
         new int[] { 2, 4 },
         new int[] { 3, 6 },
         new int[] { 4, 8 }
      };
      
      // Length of main array (number of rows)
      Console.WriteLine("Length: " + arr.Length);
      Console.WriteLine("Upper Bound: {0}", arr.GetUpperBound(0).ToString());
      Console.WriteLine("Lower Bound: {0}", arr.GetLowerBound(0).ToString());
      Console.WriteLine("Dimensions of Array: " + arr.Rank);
   }
}

The output of the above code is −

Length: 5
Upper Bound: 4
Lower Bound: 0
Dimensions of Array: 1

Finding Individual Row Lengths

Example

using System;

public class Program {
   public static void Main() {
      int[][] jaggedArray = new int[][] {
         new int[] { 1, 2, 3 },
         new int[] { 4, 5 },
         new int[] { 6, 7, 8, 9 },
         new int[] { 10 }
      };
      
      Console.WriteLine("Main array length: " + jaggedArray.Length);
      Console.WriteLine("Individual row lengths:");
      
      for (int i = 0; i < jaggedArray.Length; i++) {
         Console.WriteLine("Row {0} length: {1}", i, jaggedArray[i].Length);
      }
   }
}

The output of the above code is −

Main array length: 4
Individual row lengths:
Row 0 length: 3
Row 1 length: 2
Row 2 length: 4
Row 3 length: 1

Key Properties Explained

Property/Method Description Return Value
Length Number of elements in the first dimension (rows) Integer
GetLowerBound(0) Starting index of the first dimension Always 0
GetUpperBound(0) Ending index of the first dimension Length - 1
Rank Number of dimensions Always 1 for jagged arrays

Conclusion

Jagged arrays in C# are arrays of arrays with variable row lengths. Use the Length property to find the number of rows, GetUpperBound(0) and GetLowerBound(0) for index bounds, and arrayName[i].Length to get individual row lengths. The Rank property always returns 1 for jagged arrays since they are single-dimensional arrays containing references to other arrays.

Updated on: 2026-03-17T07:04:35+05:30

464 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements