Table of contents
1.
Introduction
2.
One-Dimensional Jagged Array
2.1.
Direct Method
2.2.
Short Hand Method
2.3.
Example
3.
Multi-Dimensional Jagged Array
3.1.
Example
4.
Frequently Asked Questions
4.1.
What is C#?
4.2.
What is an array?
4.3.
What is the syntax to create an array in C#?
4.4.
What are jagged arrays in C#?
4.5.
What are the applications of C#?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Jagged Arrays in C#

Author Shaurya Singhal
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C# is a multi-paradigm, general programming language. C# was designed by Anders Hejlsberg in 2000. Later it was introduced by Microsoft along with the .NET framework and Visual Studio.

The array is nothing but a collection of elements of a similar data type. Each element stored in an array can be accessed easily using only the index of the element. 

A jagged array can be defined as a collection of two or more arrays of similar data types, such that each array in a Jagged array may or may not be of the same size. We can even add multi-dimensional arrays in Jagged arrays.

One-Dimensional Jagged Array

A one-dimensional jagged array is a collection of two or more one-dimensional arrays of the same data type. We can declare a jagged array using the following syntax,

Direct Method

int[][] jag_arr = new int[][] 
{
    new int[] {1, 5, 7, 9},
    new int[] {10, 25, 90},
    new int[] {15, 2},
    new int[] {1, 10, 5, 0, 50}
};

Short Hand Method

int[][] jag_arr = 
{
    new int[] {1, 5, 7, 9},
    new int[] {10, 25, 90},
    new int[] {15, 2},
    new int[] {1, 10, 5, 0, 50}
};

Example

Now, let us look at an example code of a one-dimensional jagged array. 

using System;
class JaggedArray {
public static void Main()
{   
    int[][] jag_arr = 
        {
            new int[] {1, 5, 7, 9},
            new int[] {10, 25, 90},
            new int[] {15, 2},
            new int[] {1, 10, 5, 0, 50}
        };  
for (int n = 0; n < jag_arr.Length; n++) {
System.Console.Write("Row({0}): ", n);
for (int k = 0; k < jag_arr[n].Length; k++) {
System.Console.Write("{0} ", jag_arr[n][k]);
}
System.Console.WriteLine();
}
}
}

 

In the above code, we created a jagged array using the shorthand method and then used a for loop to print all the elements present in the jagged array.

Also see, Introduction to C++

Multi-Dimensional Jagged Array

We can also add multi-dimensional arrays to a jagged array. Let us look at the syntax to declare a multi-dimensional jagged array,

int[][, ] jag_arr = new int[4][, ] {
new int[, ] {{2, 4}, {1, 5}},
new int[, ] {{10, 20}, {15, 34}, {19, 13}},
new int[, ] {{11, 80}, {82, 98}, {11, 66}},
new int[, ] {{45, 73}, {71, 54}, {5, 19}}
};

In this code snippet, we have created a jagged array containing four multi-dimensional arrays.

Example

Let us look at an example of a multi-dimensional jagged array.

using System;
class MultiJaggedArray {
public static void Main(){
int[][, ] jag_arr = new int[4][, ] {
new int[, ] {{2, 4}, {1, 5}},
new int[, ] {{10, 20}, {15, 34}, {19, 13}},
new int[, ] {{11, 80}, {82, 98}, {11, 66}},
new int[, ] {{45, 73}, {71, 54}, {5, 19}}
};
for (int i = 0; i < jag_arr.Length; i++)
{
int x = 0;
for (int j = 0; j < jag_arr[i].GetLength(x); j++)
{
for (int k = 0; k < jag_arr[j].Rank; k++)
Console.Write("Jagged_Array[" + i + "][" + j + ", " + k + "]: "+ jag_arr[i][j, k] + " ");
Console.WriteLine();
}
x++;
Console.WriteLine();
}
}
}

 

In the above example, we have created a jagged containing four multi-dimensional arrays. We then used a for loop to print out all the elements present in the jagged array.

Must Read IEnumerable vs IQueryable, singleton design pattern in c#

Frequently Asked Questions

What is C#?

C# is a multi-paradigm, general programming language. C# was designed by Anders Hejlsberg in 2000. Later it was introduced by Microsoft along with the .NET framework and Visual Studio.

What is an array?

The array is nothing but a collection of elements of a similar data type. Each element stored in an array can be accessed easily using only the index of the element.

What is the syntax to create an array in C#?

Arrays can be created using the following syntax in the C and C++ programming languages,

data_type[][] name_of_array = new data_type[rows][]

What are jagged arrays in C#?

A jagged array can be defined as a collection of two or more arrays of similar data types, such that each array in a Jagged array may or may not be of the same size. We can even add multi-dimensional arrays in Jagged arrays.

What are the applications of C#?

C# has a wide range of applications. It is used for Web development, Application Development, and Game Development.

Conclusion

This blog covered all the necessary points associated with the jagged arrays in C#. We further looked at various methods of declaration of the jagged arrays and also looked at a few examples of the jagged arrays in C#.

Do check out our blogs on object-oriented programming and data structures

Recommended problems -

 

Don’t stop here. Check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass