Introduction to Multidimensional Arrays in C#
In C#, the rectangular arrays or multidimensional arrays refer to the organization of the elements as 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 are the syntax of Multidimensional Arrays:
Declaration of 2D array.
int[,] x=new int[1,2];
Declaration of 3D array.
int[,,] x=new int[1,2,3];
The syntax above specifies the format to declare two dimensional and 3-dimensional array (x). the first array contains two elements 1 and 2 whereas the three-dimensional array contains the elements 1,2,3.
Initialization of the Multidimensional Arrays
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 which includes the use of array type, array size and the use of 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 } };
Examples of C# multidimensional array
Below are the examples of Multidimensional Arrays in C#:
Example #1
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:
Example #2
Program to illustrate the initialization, declaration of a two-dimensional array and accessing 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.
Example #3
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 of the elements in the first array being added to the counter element of the second array. For example, the first element in the array1 is 1 and similarly, the first element in array2 is 9. The resultant of addition should contain an array with the first element as 10.
Advantages & Disadvantages
Below are the advantages and disadvantage of Multidimensional Arrays:
Advantages
- Multidimensional arrays can be used to organize subgroups of data within an array, additionally, the multidimensional arrays can also be used to store memory addresses of data in a pointer array.
- Multidimensional arrays have a static size and initialized at the beginning of the program. Any extension in the size shall require the relevant size to be specified during initialization.
- Multidimensional arrays can be used in performing matrix operations and maintain the large value of data under the same variable allocation.
- Multidimensional arrays find maximum use in the implementation of stacks, heaps and Queues, and hash tables.
Disadvantages
- The elements are located in contiguous memory locations for an array hence any insertion and deletion of element shall be more complex in comparison to the similar operations on single elements.
- Also, the elements cannot be inserted into the middle of an array.
- Sometimes static memory allocation can have a negative impact as allocation of memory which is more than that required is wasted and not released.
- The major disadvantage of a multidimensional array, when compared to arrays in C#, is upon the fact that multidimensional arrays are slower when compared to their array counterpart. To overcome this we can use jagged arrays as a replacement for multidimensional array.
Recommended Articles
This is a guide to C# Multidimensional Arrays. Here we discuss a brief overview on Multidimensional Arrays in C# and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –