Jagged Arrays in C#

Jagged arrays in C# are a type of array that can store arrays of different lengths. This makes them ideal for handling irregular data structures and dynamic datasets. For example, you could use a jagged array to store a list of sentences of different lengths.

Declaring and Initializing Jagged Arrays:

To declare a jagged array, use square brackets for each dimension. After declaring the jagged array, initialize each inner array separately. For example:

// Declare a jagged array of integers.
int[][] jaggedArray = new int[3][];
 
// Initialize the inner arrays.
jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9 };
Accessing Elements of a Jagged Array:

You can access individual elements of a jagged array using multiple index values. For example:

int element = jaggedArray[1][1]; // Accesses the element at row 1 and column 1
Implementation of Jagged Arrays:

Here is source code of the C# Program to Demonstrate Jagged Arrays. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

advertisement
/*
 * C# Program to Demonstrate Jagged Arrays
 */
 
using System;
class Program
{
    static void Main()
    {
        int[][] jag = new int[3][];
        jag[0] = new int[2];
        jag[0][0] = 11;
        jag[0][1] = 12;
        jag[1] = new int[1] {11};
        jag[2] = new int[3] { 14,15, 16 };
        for (int i = 0; i < jag.Length; i++)
        {
            int[] innerArray = jag[i];
            for (int a = 0; a < innerArray.Length; a++)
            {
                Console.WriteLine(innerArray[a] + " ");
            }
        }
        Console.Read();
    }
}
Program Explanation

1. This C# program demonstrates Jagged Arrays.
2. The elements in a jagged array can have different dimensions and sizes, also known as an “array of arrays“.
3. Create a new array in the jagged array, assign its values, and form the second and third rows using array initialization.
4. Jagged Arrays efficiently store multiple rows of varying lengths with any data type. A for loop prints the jagged array elements.

Program Output:
 
11
12
11
14
15
16
Method 2: C# Program to Find the Length of Jagged Array using Predefined Functions

This C# program calculates and displays the length of each row in the jagged array using predefined functions.

Free 30-Day Java Certification Bootcamp is Live. Join Now!
Program/Source Code

Here is source code of the C# Program to Find the Length of the Jagged Array using Predefined Functions. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Find the Length of the Jagged Array using Predefined Functions
 */
using System;
class Program
{
    public static void Main()
    {
        byte[][] numbers = new byte[5][];
        for (int i = 0; i < numbers.Length; i++)
        {
            numbers[i] = new byte[i + 3];
        }
        for (int i = 0; i < numbers.Length; i++)
        {
            Console.WriteLine("Length of row {0} is {1}", i, numbers[i].Length);
        }
        Console.Read();
    }
}
Program Explanation

1. Declare a jagged array numbers with five inner arrays (rows).
2. Initialize the inner arrays with varying lengths using a for loop, where each row’s length is i + 3.
3. The first inner array (row 0) has a length of 3, the second (row 1) has 4, and so on.
4. Use another for loop to iterate through the numbers array and print the length of each row using Console.WriteLine.

Program Output:
 
Length of row 0 is 3
Length of row 1 is 4
Length of row 2 is 5
Length of row 3 is 6
Length of row 4 is 7

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.