C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3 or n-dimensional. In C#, it is also known as Rectangular arrays. Let’s see the declaration of 2D and 3D arrays. int[,] arr=new int[3,3];//declaration of 3X3 array int[,,] arr=new int[3,3,3]; //declaration of 3X3X3 array C# Multidimensional array Example:

using System;
public class ArrayExample
{
public static void Main (string[] args)
{
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 },{7,8,9}};//declaration and initialization
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}
Output 1 2 3 4 5 6 7 8 9 C# Multidimensional Array Example: sum of 2 3X3 matrices The following Example declare and initialize two 3X3 array and calculate sum of them.
using System;
public class ArrayExample
{
            public static void Main (string[] args)
            {
                        int[,] arr1={{1,2,3},{4,5,6},{7,8,9}};
                        int[,] arr2={{10,11,12},{13,14,15},{16,17,18}};
                        int[,] arr3=new int[3,3];
                        for(int i=0;i<=2;i++)
                        {
                                    for(int j=0;j<=2;j++)
                                    {
                                                arr3[i,j]=arr1[i,j]+arr2[i,j];
                                    }
                        }
                        for(int i=0;i<=2;i++)
                        {
                                    for(int j=0;j<=2;j++)
                                    {
                                                Console.Write(arr3[i,j]+" ");
                                    }
                                    Console.WriteLine();
                        }
            }
}
Output 11 13 15 17 19 21 23 25 27