Jagged arrays are a special type of arrays that can be used to store rows of data of varying lengths to improve performance when working with multi-dimensional arrays An array may be defined as a sequential collection of elements of the same data type. The elements of an array are stored in contiguous memory locations. Arrays can be single or multi-dimensional. A jagged array is a special type of a multi-dimensional array in which each of the array (a jagged array is actually an array of arrays) can be of varying size. You can have jagged arrays in any computer language that provides support for arrays. A jagged array (also known as a ragged array) is an array of arrays in which the member arrays in turn can be of different dimensions and sizes. You may implement multi-dimensional arrays are jagged arrays to improve performance. Getting started with jagged arrays in C# In this section we will explore how to declare, initialize and access jagged arrays. As we know, a jagged array comprises of an array of arrays of similar or different sizes. In other words, in a jagged array, the number of rows is fixed, but the number of columns may vary. When declaring a jagged array, you can just declare the number of rows of the array and prefer to specify the number of columns at runtime. Let us know understand all that we have learnt so far on jagged arrays with a few code examples. Consider the following array. string[][] str = new string[5][]; You have declared the rows of the array. There are 5 rows in this array that can in turn contain 5 string arrays of different lengths. Let’s now see how we can declare 5 arrays in the array named str, each of different lengths. The following code snippet illustrates how this can be achieved. str[0] = new string[5]; str[1] = new string[10]; str[2] = new string[20]; str[3] = new string[50]; str[4] = new string[10]; You can now store strings of dissimilar lengths in the jagged array as shown in the code snippet below. str[0][0] = "Pune"; str[1][0] = "Kolkata"; str[2][0] = "Bangalore"; str[3][0] = "The pink city named Jaipur"; str[4][0] = "Hyderabad"; Here’s the complete code listing that illustrates how you can declare a jagged array, store data and then retrieve and display it in the console. public static void Main(string[] args) { //First declare the jagged array string[][] str = new string[5][]; str[0] = new string[5]; str[1] = new string[10]; str[2] = new string[20]; str[3] = new string[50]; str[4] = new string[10]; //Now store data in the jagged array str[0][0] = "Pune"; str[1][0] = "Kolkata"; str[2][0] = "Bangalore"; str[3][0] = "The pink city named Jaipur"; str[4][0] = "Hyderabad"; //Lastly, display the content of each of the string arrays inside the jagged array for (int i = 0; i < 5; i++) Console.WriteLine(str[i][0]); Console.Read(); } As you can see in the above program, the number of rows of the jagged array is fixed but the number of columns vary. This example represents a two – dimensional jagged array. If you were to use a normal two – dimensional array, you would have to consume 5 x 50, i.e., 250 bytes. The reason is that you would have to have space of 50 bytes in each of the arrays in the jagged array to accommodate the largest string. In this example, the largest string is of size 50. On the contrary, in using a jagged array, you end up consuming just 95 bytes! Interesting, isn’t it? When you execute the above program, the strings stored in the jagged array are displayed in the console window. Another example — a jagged array of integers Similar to how we have created jagged array of strings, you can also create jagged array of integers. In fact, you can have a jagged array of any data type. Here’s how you can declare a jagged array in C#. int [][] numbersArray; The following code snippet illustrates how you can declare an integer jagged array, i.e., a jagged array that can in turn store arrays of integers of varying elements. int[][] numbersArray = new int[5][]; for (int i = 0; i < numbersArray.Length; i++) { numbersArray[i] = new int[10 * (i + 1)]; } The above code snippet creates an integer jagged array named numbersArray which in turn contains integer arrays of varying sizes. Related content how-to How to use FastEndpoints in ASP.NET Core Take advantage of the free open-source FastEndpoints library to build fast and lean APIs in your ASP.NET Core applications. By Joydip Kanjilal Jul 11, 2024 7 mins Microsoft .NET C# Development Libraries and Frameworks how-to How to use Refit to consume APIs in ASP.NET Core Take advantage of Refit REST library to simplify API consumption and make your code cleaner, more efficient, and easier to maintain. By Joydip Kanjilal Jul 04, 2024 10 mins C# Microsoft .NET Software Deployment how-to When to use an abstract class vs. interface in C# Understanding the differences between an abstract class and interface is key to designing loosely coupled and extensible applications. By Joydip Kanjilal Jun 20, 2024 10 mins Small and Medium Business Microsoft .NET C# how-to 6 security best practices for ASP.NET Core Learn the best practices and built-in safeguards for preventing attacks and protecting sensitive data in your ASP.NET Core web applications. By Joydip Kanjilal Jun 07, 2024 6 mins C# Microsoft .NET Web Development Resources Videos