In C#, the rectangular arrays or multidimensional arrays refer to the organization of the elements in a matrix format. A multidimensional array can only be of two or three dimensions. Dimensions of an array refer to the organization format of the data in the variable. Thus we can define a multidimensional array as an organization of elements in series or sequence as rows or columns.
Syntax:
Below is the syntax of Multidimensional Arrays:
Declaration of the 2D array.
int[,] x=new int[1,2];
Declaration of the 3D array.
int[,,] x=new int[1,2,3];
The syntax above specifies the format to declare two-dimensional and 3-dimensional arrays (x). the first array contains two elements, 1 and 2, whereas the three-dimensional array contains the element 1,2,3.
A multidimensional array can be initialized in three different ways
1. Complete Declaration
int[,] x = new int[6,6];
The above specification initializes a two-dimensional array completely, including the use of array type, array size, and the new operator.
2. Initialising without Using the New Operator
int[,] x = { { 3,2,1 }, { 6,5,4 }, { 9,8,7 } };
3. Initializing the Array without Declaring the Size
int[,] x = new int[,]{ { 3,2,1 }, { 6,5,4 }, { 9,8,7 } };
Below are the examples of Multidimensional Arrays in C#:
Program to illustrate declaring and initialization of a multidimensional array. The example below illustrates the creation of a multidimensional array in C#.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { public static void Main(string[] args) { int[,] x = { { 3, 2, 1 }, { 6, 5, 4 }, { 9, 8, 7 } }; for (int a = 0; a < 3; a++) { for (int b = 0; b < 3; b++) { Console.Write(x[a, b] + " "); } Console.WriteLine(); } } } }
Output:
Program to illustrate the initialization, declaration of a two-dimensional array, and access the elements.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { /* declaring and initialising a two dimensional array*/ int[,] b = new int[6, 2] { { 1, 2 }, { 4, 3 }, { 5, 6 }, { 8,7 }, { 9 , 10 }, { 2, 3 } }; int i, j; /* accessing each of the elements value for the array */ for (i = 0; i < 6; i++) { for (j = 0; j < 2; j++) { Console.WriteLine("a[{0},{1}] = {2}", i, j, b[i, j]); } } Console.ReadKey(); } } }
Output:
The program above demonstrates the use of indices as a positional marker for accessing the elements of the array in a multidimensional array.
Program for the addition of two multidimensional arrays.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { public static void Main() { int[,] array1 = new int[3, 3]; int[,] array2 = new int[3, 3]; int[,] resultArray = new int[3, 3]; int i, j; Console.WriteLine("specify the members of the first array: "); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { array1[i, j] = Convert.ToInt32(Console.ReadLine()); } } Console.WriteLine("elements of the array1: "); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("{0} ", array1[i, j]); } Console.Write("\n"); } Console.WriteLine("specify the members of the array2: "); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { array2[i, j] = Convert.ToInt32(Console.ReadLine()); } } Console.WriteLine("elements of the array2: "); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("{0} ", array2[i, j]); } Console.Write("\n"); } for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { resultArray[i, j] = array1[i, j] + array2[i, j]; } } Console.WriteLine("resultArray of the array1 and array2 looks as below : "); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("{0} ", resultArray[i, j]); } Console.Write("\n"); } } } }
Output:
Using the program above, we have completed the addition operation on the array, with each element in the first array added to the second array’s counter element. For example, the first element in array1 is 1; similarly, the first element in array2 is 9. The resultant of addition should contain an array with the first element as 10.
Below are the advantages and disadvantages of Multidimensional Arrays:
The above is the detailed content of C# Multidimensional Arrays. For more information, please follow other related articles on the PHP Chinese website!