In c#, Jagged Array is an array whose elements are arrays with different dimensions and sizes. Sometimes a jagged array is called an “array of arrays” and can store arrays instead of a particular data type value.
In c#, a jagged array can be initialized with two square brackets [][]. The first square bracket will specify the size of an array, and the second one will specify the dimension of the array, which will be stored as a value.
The following are examples of creating jagged arrays in c# programming language with single and multidimensional arrays.
If you observe the above examples, the first array (jarray) is allowed to store 2 elements of single-dimensional arrays. The second array (jarray1) is allowed to store 3 elements of multidimensional arrays.
In c#, we can initialize arrays upon declaration. The following are the different ways of declaring and initializing jagged arrays in the c# programming language.
If you observe the above examples, we declared and initialized jagged arrays with single-dimensional and multidimensional arrays as elements in different ways.
In jagged arrays, the size of elements is optional. In some of the elements, we didn’t mention a size to store single or multidimensional arrays with different sizes based on our requirements.
In c#, we can access jagged arrays' values by using a row index and column index values.
Following is the example of accessing elements from jagged arrays in c# programming language based on our requirements.
If you observe the above example, we are accessing single or multidimensional arrays by passing index values.
Following is the example of using jagged arrays in c# programming language to represent arrays as elements in an array with single or multiple dimensions.
If you observe the above example, we are creating a jagged array with single & multidimensional arrays, and we used for loop in c# to get the elements of the jagged array.
When we run the above c# program, we will get the result below.
If you observe the above result, we stored arrays as elements in a jagged array based on our requirements.
This is how we can use jagged arrays in our c# applications to store single or multi-dimensional arrays as an element of the jagged array.