Two-Dimensional Array in C#

Two-Dimensional Array in C# with Examples

In this article, I am going to discuss the Two-Dimensional Array in C# with Examples. Please read our previous article before proceeding to this article where we discussed one-dimensional Arrays in C# with examples.  As part of this article, we are going to discuss the following pointers.

  1. What is Two-Dimensional Array in C#?
  2. Understanding the rectangular and jagged array in C# with examples.
What is Two-Dimensional Array in C#?

The arrays which store the elements in the form of rows and columns are called Two-Dimensional Array in C#. The two-dimensional array you can also say multidimensional array are of two types in C#. They are as follows

  1. Rectangular array: The array whose rows and columns are equal are called rectangular array
  2. Jagged array: The array whose rows and columns are not equal are called jagged array
Rectangular Two-Dimensional Array in C#:

Let us first understand the syntax of Two-Dimensional Array in C#. Please have a look at the following diagram.

Rectangular Two-Dimensional Array in C#

Let us see an example for a better understanding of the rectangular array in C#.

namespace TwoDimensionalArayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr = new int[4, 5];
            int a = 0;

            //printing the values of 2d array using foreach loop
            //It will print the default values as we are not assigning
            //any values to the array
            foreach (int i in arr)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine("\n");

            //assigning values to the array by using nested for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    a += 5;
                    arr[i, j] = a;
                }
            }

            //printing the values of array by using nested for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j] + " ");
                }
            }
            Console.ReadKey();
        }
    }
}

OUTPUT:

two-dimensional array in C#

In the above example, we assigned the two-dimensional array element using nested for loop. It is also possible that we can assign the values to two-dimensional array in C# at the time of its declaration:

Assigning values to two-dimensional array in C# at the time of declaration:
namespace TwoDimensionalArayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Assigning the array elements at the time of declaration
            int[,] arr = {{11,12,13,14},
                          {21,22,23,24},
                          {31,32,33,34}};

            //printing values of array using for each loop
            foreach (int i in arr)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine("\n");

            //printing the values of array using nested for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write(arr[i, j] + " ");
                }
            }

            Console.ReadKey();
        }
    }
}

OUTPUT:

two-dimensinal Arrays in C#

Jagged Array in C#:

These are also two-dimensional array which will also store the data in the forms of rows and columns. But here in the jagged array, the column size will differ from row to row. That means if the first row contains 5 columns then the second row may contain 4 columns while the third row may contain 10 columns. So the point that you need to remember is if the column size varies from row to row then it is a jagged array. If the column size remains the same for all the rows then it is a rectangular two-dimensional array.

The jagged array in C# is also called the array of arrays. This is because in the case of the jagged array each row is a single dimensional array. So a combination of multiple single-dimensional arrays with different column sizes form a jagged array in C#.

Syntax:  <type> [][] <name> = new <type> [rows][];

Example:

int [][] arr = new int[3][];
//Or
int [][] arr = {list of values};

To declare a jagged array in C#, at the time of its declaration, you only need to specify the number of rows that you want in the array. for example

int [][] arr = new int[4][];

In the above array declaration, we are specifying that we want four rows in the array. Once you specify the number of rows that you want in the array, then you need to initialize each row with the number of columns by using a single dimensional array as shown below.

arr[0] = new int[5]; // we want five columns in the first row
arr[1] = new int[6]; // we want six columns in the first row
arr[2] = new int[4]; // we want four columns in the first row
arr[3] = new int[5]; // we want five columns in the first row

Example of the jagged array in C#:
namespace TwoDimensionalArayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating an jagged array with four rows
            int[][] arr = new int[4][];

            //Initializing each row with different column size
            // Uisng one dimensional array
            arr[0] = new int[5];
            arr[1] = new int[6];
            arr[2] = new int[4];
            arr[3] = new int[5];

            //printing the values of jagged array using nested for loop
            //It will print the default values as we are assigning any
            //values to the array
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                {
                    Console.Write(arr[i][j] + " ");
                }
            }
            Console.WriteLine();

            //assigning values to the jagged array by using nested for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                {
                    arr[i][j] = j++;
                }
            }

            //print values the values of jagged array by using foreach loop within for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                foreach (int x in arr[i])
                {
                    Console.Write(x + " ");
                }
            }
            Console.ReadKey();
        }
    }
}

When we run the application, it will give us the following output:

Jagged Array in C#

In the above example, we are assigning the elements of the jagged array by using nested for loop. It is also possible to assign the values of the jagged array at the time of its declaration.

Assigning jagged array at the time of its declaration:
namespace TwoDimensionalArayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Assigning the values of the jagged array
            // at the time of its declaration
            int[][] arr = {
                            new int[4]{11,12,13,14},
                            new int[5]{21,22,23,24,25},
                            new int[3]{31,32,33}
                           };

            //printing the values of jagged array by using nested for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr[i].Length; j++)
                {
                    Console.Write(arr[i][j] + " ");
                }
            }
            Console.WriteLine();

            //print the values of jagged array by using foreach loop within for loop
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                foreach (int x in arr[i])
                {
                    Console.Write(x + " ");
                }
            }
            Console.ReadKey();
        }
    }
}
OUTPUT:

Assigning jagged array at the time of its declaration

In the next article, I am going to discuss the advantages and disadvantages of arrays in C# with examples. Here, in this article, I try to explain the Two-Dimensional Array in C# with examples. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *