0
0
CsharpComparisonBeginner · 4 min read

2D Array vs Jagged Array in C#: Key Differences and Usage

In C#, a 2D array is a rectangular grid where each row has the same number of columns, while a jagged array is an array of arrays where each inner array can have different lengths. 2D arrays use contiguous memory, making access faster, but jagged arrays offer more flexibility for uneven data.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of 2D arrays and jagged arrays in C#.

Aspect2D ArrayJagged Array
StructureRectangular grid (rows × columns)Array of arrays (rows can vary in length)
Memory LayoutContiguous block of memorySeparate memory blocks for each inner array
Access Syntaxarray[row, column]array[row][column]
FlexibilityFixed size for all rowsRows can have different lengths
PerformanceFaster access due to contiguous memorySlightly slower due to multiple references
Use CaseUniform data like matricesNon-uniform data like ragged tables
⚖️

Key Differences

A 2D array in C# is declared with a comma inside square brackets, like int[,] array = new int[3,4];. It creates a rectangular grid where every row has the same number of columns. This layout uses a single continuous block of memory, which makes accessing elements fast and predictable.

On the other hand, a jagged array is an array of arrays, declared as int[][] jagged = new int[3][];. Each inner array can have a different length, so the rows can be uneven. This flexibility is useful when data naturally varies in size, but it uses multiple memory blocks and requires two indexing steps.

Accessing elements differs: 2D arrays use array[row, column] syntax, while jagged arrays use array[row][column]. Because jagged arrays store references to inner arrays, they can be slightly slower to access but allow more dynamic structures.

⚖️

Code Comparison

This example shows how to create and print a 2D array with 3 rows and 4 columns.

csharp
using System;

class Program
{
    static void Main()
    {
        int[,] array2D = new int[3, 4]
        {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };

        for (int i = 0; i < array2D.GetLength(0); i++)
        {
            for (int j = 0; j < array2D.GetLength(1); j++)
            {
                Console.Write(array2D[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
}
Output
1 2 3 4 5 6 7 8 9 10 11 12
↔️

Jagged Array Equivalent

This example creates and prints a jagged array with 3 rows, where each row can have different lengths.

csharp
using System;

class Program
{
    static void Main()
    {
        int[][] jaggedArray = new int[3][];
        jaggedArray[0] = new int[] {1, 2, 3};
        jaggedArray[1] = new int[] {4, 5};
        jaggedArray[2] = new int[] {6, 7, 8, 9};

        for (int i = 0; i < jaggedArray.Length; i++)
        {
            for (int j = 0; j < jaggedArray[i].Length; j++)
            {
                Console.Write(jaggedArray[i][j] + " ");
            }
            Console.WriteLine();
        }
    }
}
Output
1 2 3 4 5 6 7 8 9
🎯

When to Use Which

Choose a 2D array when your data is naturally rectangular and fixed in size, such as matrices or grids, because it offers better performance and simpler syntax. Use a jagged array when your data rows vary in length or you need more flexibility, like storing lists of different sizes or ragged tables. Jagged arrays are also useful when you want to save memory by not allocating unused elements in shorter rows.

Key Takeaways

Use 2D arrays for fixed-size, rectangular data for faster access and simpler code.
Use jagged arrays for flexible, uneven row sizes and memory savings.
2D arrays use contiguous memory; jagged arrays use multiple references.
Access syntax differs: array[row, column] vs array[row][column].
Choose based on your data shape and performance needs.