The Wayback Machine - https://web.archive.org/web/20261109154017/https://www.geeksforgeeks.org/c-sharp/jagged-arrays-in-c-sharp/

Jagged Arrays in C#

Last Updated : 9 Oct, 2026

A jagged array is an array of arrays where each inner array can have a different length. Unlike a multidimensional array, which has a rectangular structure, a jagged array allows rows to contain different numbers of elements.

  • Each element of the outer array holds a reference to an inner array.
  • Inner arrays can have different lengths and are initialized individually.
  • Jagged arrays are useful for storing irregular data, such as student marks or variable-length records.

Example: The following program creates a jagged array with three rows of different lengths and displays its elements.

C#
using System;

class Geeks{
    static void Main(){
        
        // Create a jagged array with 4 rows
        int[][] jagged_arr = new int[4][];

        // Initialize rows with different lengths
        jagged_arr[0] = new int[] { 1, 2, 3, 4, 5, 6, 7 };
        jagged_arr[1] = new int[] { 8, 9, 10, 11, 12, 13 };
        jagged_arr[2] = new int[] { 14, 15, 16 };
        jagged_arr[3] = new int[] { 19, 20, 21, 22, 23 };

        // Display the elements
        for (int i = 0; i < jagged_arr.Length; i++) {
            Console.Write("Row " + i + ": ");

            foreach(int num in jagged_arr[i])
            {
                Console.Write(num + " ");
            }

            Console.WriteLine();
        }
    }
}

Output
Row 0: 1 2 3 4 5 6 7 
Row 1: 8 9 10 11 12 13 
Row 2: 14 15 16 
Row 3: 19 20 21 22 23 

Explanation:

  • new int[4][] creates an outer array containing four references.
  • jagged_arr[0] contains 7 elements.
  • jagged_arr[1] contains 6 elements.
  • jagged_arr[2] contains 3 elements.
  • jagged_arr[3] contains 5 elements.
  • Nested loops traverse each row and print its elements.
JaggedArray
Jagged Arrays Representation in Heap Memory

Declaration and Initialization

For declaring we specify the type of elements inside the inner arrays and the number of rows in the outer array. Unlike multi-dimensional arrays, where the size of all dimensions is fixed, a jagged array allows for variable lengths of inner arrays.

1. Declaring a Jagged Array

The following statement creates an outer array that can reference three integer arrays.

Syntax

data_type[][] arrayName = new data_type[rows][];

2. Initializing Inner Arrays

We can initialize each row separately and assign different numbers of elements.

2. Initializing Inner Arrays

We can initialize each row separately and assign different numbers of elements.

int[][] jaggedArr = new int[3][];

jaggedArr[0] = new int[] { 1, 2, 3 };
jaggedArr[1] = new int[] { 4, 5 };
jaggedArr[2] = new int[] { 6, 7, 8, 9 };

The resulting array contains three rows with lengths of 3, 2, and 4, respectively.

3. Declaration with Initialization

We can also declare and initialize a jagged array in a single statement.

Method 1: Using the new Keyword

int[][] jaggedArr = new int[][]{

new int[] { 1, 2, 3 },
new int[] { 4, 5 },
new int[] { 6, 7, 8, 9 }
};

Method 2: Using Shorthand Initialization

int[][] jaggedArr ={

new int[] { 1, 2, 3 },
new int[] { 4, 5 },
new int[] { 6, 7, 8, 9 }
};

Both methods create a jagged array containing three inner arrays of different lengths.

Example: Basic program to implement jagged array

C#
using System;

class Geeks{
    static void Main(){
        // Declaration  
        int[][] jaggedArray = new int[3][];
      
      	// Initialization
        jaggedArray[0] = new int[] { 1, 2 };
        jaggedArray[1] = new int[] { 3, 4, 5 };
        jaggedArray[2] = new int[] { 6, 7, 8, 9 };

      	// Iterating the elements
        for (int i = 0; i < jaggedArray.Length; i++) 
        {
            Console.Write("Row " + i + ": ");
            foreach(int num in jaggedArray[i])
                Console.Write(num + " ");
          
            Console.WriteLine();
        }
    }
}

Output
Row 0: 1 2 
Row 1: 3 4 5 
Row 2: 6 7 8 9 

Operations on Jagged Arrays

Jagged arrays support operations such as accessing, modifying, finding the length, and iterating through elements using indexes and loops.

1. Accessing and Modifying Elements

Elements in a jagged array are accessed using two indexes. The first index specifies the row, while the second specifies the element within that row.

C#
using System;

class Geeks{
    static void Main(string[] args){
        // Declaration of a jagged array with 3 rows
        int[][] arr = new int[3][];
        
        // Initializing each row of the jagged array First row
        arr[0] = new int[] { 1, 2, 3, 4}; 
      
      	// Second row
        arr[1] = new int[] { 4, 5, 6 }; 
      
      	// Third row (only two elements)
        arr[2] = new int[] { 7, 8 };    

        // Accessing the third element in second row
        int value = arr[1][2]; 
        
        // Modifying the second element in the first row (from 2 to 10)
        arr[0][1] = 10; 
        
        // Outputting the modified value
        Console.WriteLine(arr[0][1]);  

        // Outputting the accessed value for verification
        Console.WriteLine("Accessed value: " + value);
    }
}

Output
10
Accessed value: 6

Explanation:

  • arr[1][2] accesses the third element of the second row, which is 6.
  • arr[0][1] = 10 changes the second element of the first row from 2 to 10.

2. Finding the Length of a Jagged Array

The Length property is used to determine the number of rows in a jagged array and the number of elements in each initialized row.

  • arr.Length returns the number of rows in the outer array.
  • arr[i].Length returns the number of elements in the inner array at index i.
C#
using System;

class GFG
{
    static void Main()
    {
        int[][] arr =
        {
            new int[] { 1, 2, 3 },
            new int[] { 4, 5 },
            new int[] { 6, 7, 8, 9 }
        };

        Console.WriteLine("Number of rows: " + arr.Length);
        Console.WriteLine("Elements in first row: " + arr[0].Length);
        Console.WriteLine("Elements in second row: " + arr[1].Length);
    }
}

Output
Number of rows: 3
Elements in first row: 3
Elements in second row: 2

3. Iterating Through a Jagged Array

We can traverse a jagged array using nested loops. The outer loop selects each row, while the inner loop accesses its elements.

Example: Iterating through a jagged array using nested for loops.

C#
using System;
class Geeks {
    static void Main()
    {
        int[][] arr
            = { new int[] { 1, 2 }, new int[] { 3, 4, 5 },
                new int[] { 6, 7, 8, 9 } };
        for (int i = 0; i < arr.Length; i++) {
            Console.Write("Row " + i + ": ");
            for (int j = 0; j < arr[i].Length; j++) {
                Console.Write(arr[i][j] + " ");
            }
            Console.WriteLine();
        }
    }
}

Output
Row 0: 1 2 
Row 1: 3 4 5 
Row 2: 6 7 8 9 

Note: Accessing an invalid index throws an IndexOutOfRangeException. Accessing an element through an uninitialized inner array throws a NullReferenceException.

Advantages of Jagged Arrays

  • Flexible Structure: Each inner array can contain a different number of elements.
  • Memory Efficiency: Can avoid allocating unused elements when rows have different lengths.
  • Independent Rows: Inner arrays can be replaced individually without recreating the outer array.
  • Easy Traversal: Supports standard for and foreach loops.

Applications of Jagged Arrays

Jagged arrays are useful when different groups of data contain varying numbers of elements.

  • Student Marks: Store marks for students taking different numbers of subjects.
  • Graph Representation: Represent adjacency lists where each vertex has a different number of neighbors.
  • Variable-Length Records: Store records containing different numbers of values.
  • Triangular Data: Represent structures where each row contains a different number of elements.
Comment

Explore