Jagged Arrays in C#

Learn via video courses
Topics Covered

Overview

An array is a fundamental data structure in programming that allows you to store a group of elements of the same data type in contiguous memory locations. It allows you to organize and access multiple items of data using a single variable name. Arrays are available in a variety of computer languages, including C#, Java, Python, C++, and others. For example, if we want to store a student's grades in five subjects, we don't need to create separate variables for every subject. Rather, we can create an array that will store the data items in contiguous memory locations.

Introduction to Jagged Array in C#

In C#, a jagged array is a multidimensional array in which every element of the main array is itself an array. A jagged array, unlike the traditional multidimensional array, allows the inner arrays to be of differing lengths, giving it a versatile and strong data structure for dealing with irregular or uneven data. One of the unique characteristics of jagged arrays is that their elements are reference types that are initialized to null by default. This means that when you create a jagged array, its elements are not assigned any specific values unless you manually initialize them with individual arrays.

Declaration

In jagged arrays, the user is only required to specify the number of rows and not the number of columns. If the user also specifies the number of columns, the array loses its jagged characteristic and becomes a traditional multi-dimensional array.

Syntax:

Example:

In the above example, a jagged array is declared, where int is the array's data type, jagged_array is its name, and [3][] is the number of elements (arrays) included within the jagged array. 

We can set the size of the individual array since we know each member of a jagged array is itself an array. For example,

Initialization and Filling Elements in Jagged Array

There are several ways to initialize and filling the elements of a jagged array in C#.

  1. Using the index number: After declaring a jagged array, we can use the index number to initialize it. This implies that we can dynamically set values for each member in the array by accessing its specific index position. For example,

In the above example, the index at the first square bracket indicates the index of the jagged array element, and the index at the second square bracket represents the index of the element inside each element of the jagged array.

  1. Initialize without setting the size of array elements: When the size of the jagged array is not predetermined or needed, its elements can be directly initialized with specific values using inline initialization. For example,
  1. Initialize while declaring Jagged Array: In C#, we can use array initializers directly to initialize the jagged array upon declaration. This method allows you to declare and fill the jagged array's elements in one step, making the code more compact and expressive. For example,

C# Jagged Array Example

Example of Jagged Array in C# which declares, initializes and traverses jagged arrays

Here is a C# jagged array program that declares, initializes, and traverses jagged arrays.

Output:

Example of Jagged Array which Initializes the Jagged Arrays Upon Declaration

Here is a C# jagged array example that initializes the jagged arrays upon declaration.

Output:

Accessing the Elements

To access the elements of a jagged array, use the index notation as you would with any other multi-dimensional array. However, since a jagged array is an array of arrays, you need to specify both the row and column indices precisely within the array name to access a specific element.

Example:

Here's a C# program that demonstrates how to access the elements of a jagged array:

Output:

Jagged Arrays With Multidimensional Arrays

In C#, you can use jagged arrays in conjunction with multidimensional arrays to create complex data structures. This allows the creation of arrays of arrays, with some of the elements being multi-dimensional arrays. The definition and initialization of a 1-D jagged array containing three two-dimensional array elements of varying sizes are shown below.

To access the individual elements, as seen in the following example, which displays the value of the second array's element [1, 0].

Example:

Here is a C# program that demonstrates the mixing of a 1-D Jagged Array with three 2-D arrays.

Output:

Conclusion

  • An array is a fundamental data structure in programming that allows you to store a group of elements of the same data type in contiguous memory locations.
  • In C#, a jagged array is a multidimensional array in which every element of the main array is itself an array.
  • A jagged array, unlike the traditional multidimensional array, allows the inner arrays to be of differing lengths, giving it a versatile and strong data structure for dealing with irregular or uneven data.
  • In the case of jagged arrays, the user is only required to specify the number of rows and not the number of columns.
  • To access the elements of a jagged array, use the index notation as you would with any other multi-dimensional array.
  • In C#, you can use jagged arrays in conjunction with multidimensional arrays to create complex data structures.