C# allows multi-dimensional arrays. This could be of 2D or 3D (two-dimensional or three-dimensional) Array. These arrays are stored in the form of a table (with rows and columns) which is also known as a matrix.
In C#, multidimensional array is also known as rectangular arrays.
To declare an array of multi-dimensions, we need to use comma (,) inside the brackets []. Such as:
1 2 3 4 5 | //Two Dimensional array (2D) int arr[,]; //Three Dimensional array (3D) int arr[, ,]; |
Initialization of Multi Dimensional Array in C#
We can initialize a Multi-Dimensional Array (two-dimensional or three-dimensional) in the following ways in C#.
1. Initialization of two-dimensional array:
2D array is the simplest of multi-dimensional array. It has rows and column like a table.
The following is the 2 by 3 matrix array that is array with 2 rows and 3 columns.
1 2 3 4 | int [,] arr = new int [2,3] { {0, 1, 2} , {3, 4, 5} }; |
2. Initialization of three-dimensional array:
1 2 3 4 5 6 7 8 | int[,, ] arr= new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12} } }; |
You can also declare and initialize multi-dimensional array in three different ways .
1 2 3 4 5 6 7 8 | //Method 1 int[,] arr = new int[2,2]= { { 1, 2 }, { 3, 4 } }; //Method 2: Omitting the array size int[,] arr = new int[,]{ { 1, 2 }, { 3, 4 } }; //Method 3 Omitting new operator int[,] arr = { { 1, 2 }, { 3, 4 } }; |
Accessing two Dimensional array in C#
Accessing 2d Array:
Subscripts are used to access the 2D array element. It can be achieved by placing the particular index number within the two brackets [][] (that is the row index and column index). Such as:
1 2 3 | arr[2,3]; //accessing in 4D OR int val = arr[2,3]; // assigning the element to val |
Accessing 3d Array:
Accessing a 3-dimensional array is also as same as a two-dimensional array, the only difference is that in a 3d array we have to specify 3 subscripts with a required index number.
1 2 | //Accessing 3D Array arr[2,1,1]; |
Example of C# Multi Dimensional Array:
Example: Two Dimensional Array in C#
C# Program to display all elements in a 2D array. We need to use a nested loop in order to iterate through a multi-dimensional array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | using System; namespace Arrays { class Program2D { static void Main(string[] args) { // array with 4 rows and 3 columns int[, ] arr = new int[4, 3] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 10, 11 } }; //displaying for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { Console.WriteLine("arr[{0},{1}] = {2}", i, j, arr[i, j]); } } } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 | arr[0,0] = 0 arr[0,1] = 1 arr[0,2] = 2 arr[1,0] = 3 arr[1,1] = 4 arr[1,2] = 5 arr[2,0] = 6 arr[2,1] = 7 arr[2,2] = 8 arr[3,0] = 9 arr[3,1] = 10 arr[3,2] = 11 |
Example: Three Dimensional Array in C#
C# Program to display all elements in an 3D array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | using System; namespace Arrays { class Program3D { static void Main(string[] args) { // 3D array with 2,2,3 dimension int[,, ] arr = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }; //displaying for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 3; k++) { Console.WriteLine("arr[{0},{1},{2}] = {3}", i, j, k, arr[i, j, k]); } } } } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 | arr[0,0,0] = 1 arr[0,0,1] = 2 arr[0,0,2] = 3 arr[0,1,0] = 4 arr[0,1,1] = 5 arr[0,1,2] = 6 arr[1,0,0] = 7 arr[1,0,1] = 8 arr[1,0,2] = 9 arr[1,1,0] = 10 arr[1,1,1] = 11 arr[1,1,2] = 12 |
As we saw the examples of 2D and 3D arrays, in a similar we can create any number dimension as required. However, the most used multidimensional array is a two-dimensional array.