What is a Jagged Array In C#?

C# arrays can be represented in different ways including simple arrays, one-dimension arrays, multi-dimension arrays, jagged arrays, and mixed arrays. If you’re new to arrays, check out Working with Arrays in C#
 
Here is a simple array with 3 elements.
 
int[] staticIntArray = new int[3] { 1, 3, 5 };
 
Alternatively, we can also add array items one at a time, as shown below:
 
int[] staticIntArray = new int[3];
staticIntArray[0] = 1;
staticIntArray[1] = 3;
staticIntArray[2] = 5;
 
The following code snippet initializes an array of strings and reads its items back.
 
The foreach control statement is used to iterate through the items of an array.
 
int[] staticIntArray = new int[3] { 1, 3, 5 };
foreach (int i in staticIntArray)
Console.WriteLine(i);
string[] strArray = new string[] { "Mahesh Chand", "Mike Gold", "Raj Beniwal", "Praveen Kumar", "Dinesh Beniwal" };
foreach (string str in strArray)
Console.WriteLine(str);
 
Jagged arrays are an array of arrays. The elements of a jagged array are the other arrays.
 
Declaration of a jagged array involves two brackets. For example, the following code snippet declares a jagged array that has three items of an array:
 
int[][] intJaggedArray = new int[3][];
 
The following code snippet declares a jagged array with two items of an array:
 
string[][] stringJaggedArray = new string[2][];
 
Before a jagged array can be used, its items must be initialized. The following code snippet initialized a jagged array’s first item with an array of integers with two integers, the second item with an array of integers with 4 integers, and the third item with an array of integers with 6 integers:
 
// Initializing jagged arrays
intJaggedArray[0] = new int[2];
intJaggedArray[1] = new int[4];
intJaggedArray[2] = new int[6];
 
We can also initialize jagged array items by providing the values of item arrays. The following code snippet initializes item arrays direct during the declaration:
 
// Initializing jagged arrays
intJaggedArray[0] = new int[2] { 2, 12 };
intJaggedArray[1] = new int[4] { 4, 14, 24, 34 };
intJaggedArray[2] = new int[6] { 6, 16, 26, 36, 46, 56 };
 
We can access jagged array items individually in the following way:
 
Console.Write(intJaggedArray[0][0]);
Console.WriteLine(intJaggedArray[2][5]);
 
We can also loop through all items of a jagged array. The Length property of an array helps to give us the number of items in an array. The following code loops through all of the items of a jagged array and displays on the screen:
 
// Loop through all itesm of a jagged array
for (int i = 0; i < intJaggedArray.Length; i++)
{
Console.Write("Element({0}): ", i);
for (int j = 0; j < intJaggedArray[i].Length; j++)
{
Console.Write("{0}{1}", intJaggedArray[i][j], j == (intJaggedArray[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
 
Here is the complete code sample:
 
using System;
namespace JaggedArrayInCSharp
{
class Program
{
static void Main(string[] args)
{
int[][] intJaggedArray = new int[3][];
// Initializing jagged arrays
intJaggedArray[0] = new int[2] { 2, 12 };
intJaggedArray[1] = new int[4] { 4, 14, 24, 34 };
intJaggedArray[2] = new int[6] { 6, 16, 26, 36, 46, 56 };
// Loop through all items of a jagged array
for (int i = 0; i < intJaggedArray.Length; i++)
{
Console.Write("Element({0}): ", i);
for (int j = 0; j < intJaggedArray[i].Length; j++)
{
Console.Write("{0}{1}", intJaggedArray[i][j], j == (intJaggedArray[i].Length - 1) ? "" : " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
 
Summary
 
In this code example, we learned how to use jagged arrays in C#.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.