C# Multidimensional Arrays
Multidimensional arrays can be termed as arrays of arrays, extending the capabilities of one-dimensional arrays to store data in a structured format.
Syntax
data_type[1st dimension][2nd dimension][]..[Nth dimension] array_name = new data_type[size1][size2]….[sizeN];
Parameters:
- data_type: Type of data to be stored in the array. For example: int, char, etc.
- dimension: The dimension of the array created. For example: 1D, 2D, etc.
- array_name: Name of the array
- size1, size2, …, sizeN: Sizes of the dimensions respectively.
C# Two-Dimensional Arrays
Two-Dimensional Array is the first step to make an array multidimensional. 2-D array can be interpreted as a collection of multiple One-Dimensional Arrays.
Initializing and Declaring of 2-Dimensional Array
Syntax:
// Initializing
data_type[ , ] arr = new data_type[ rows , col ];
The above syntax is a good way only when we need for user input array. For other case, we can initialize and declare array together.
Example:
// Initializing and Declaration
int[,] arr= { {1,2},{3,4});
After creating the array we need make sure that we can access and iterate the elements of the Array.
Representation of 2D Array in Tabular Format

Accessing and Iterating Elements
Just like any other programming language an array can be accessed using the index of the array. For an example element at ith row and jth column can be accessed using arr_name[i][j].
Example:
// Two-Dimensional Array
using System;
public class Geeks
{
static public void Main()
{
// Array Initialized and Declared
int[, ] arr = { { 1, 2 }, { 3, 4 } };
Console.WriteLine("Elements of Arrays:");
// Iterating the Elements of Array
for (int i = 0; i < arr.GetLength(0); i++) {
for (int j = 0; j < arr.GetLength(1); j++) {
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
Output
Elements of Arrays: 1 2 3 4
Two-Dimensional Array with User input
Array is just not limited to predefined values we can take input from the user. So, to create a user input array follow the steps mentioned below:
- Step 1: Declare the Multi-Dimensional Array with fixed number of rows and columns.
- Step 2: Iterate the elements of array position and then assigning the values to it using ReadLine() Method.
- Step 3: Iterate and Printing the elements of Array.
Example:
// User Input 2D Array
using System;
public class Geeks
{
static public void Main()
{
int[, ] arr = new int[2, 3];
// Taking Input from User
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write($ "Enter arr[{i}][{j}]: ");
arr[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("Elements of Array:");
// Iterating and Printing the
// elements of 2D array
for (int i = 0; i < arr.GetLength(0); i++) {
for (int j = 0; j < arr.GetLength(1); j++) {
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
Input:
Enter arr[0][0]: 1
Enter arr[0][1]: 2
Enter arr[0][2]: 3
Enter arr[1][0]: 4
Enter arr[1][1]: 5
Enter arr[1][2]: 6
Output:
Elements of Array:
1 2 3
4 5 6
C# Three-Dimensional Array
A 3D Array is just a complex representation of Multidimensional Array. It can be said that a 3-Dimensional Array is a combination of multiple 2-Dimensional Array.
Example: Taking input and iterating the three dimensional array.
// User Input 3-D Array
using System;
public class Geeks
{
static public void Main()
{
int[, , ] arr = new int[2, 2, 2];
// Taking input for the 3D array
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
Console.Write($"Enter arr[{i}][{j}][{k}]: ");
arr[i, j, k] = int.Parse(Console.ReadLine());
}
}
}
// Displaying the 3D array
Console.WriteLine("Elements of the Array:");
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
for (int k = 0; k < arr.GetLength(2); k++)
{
Console.Write(arr[i, j, k] + " ");
}
// Newline after each depth slice
Console.WriteLine();
}
// Newline after each row
Console.WriteLine();
}
}
}
Input:
Enter arr[0][0][0]: 1
Enter arr[0][0][1]: 2
Enter arr[0][1][0]: 3
Enter arr[0][1][1]: 4
Enter arr[1][0][0]: 5
Enter arr[1][0][1]: 6
Enter arr[1][1][0]: 7
Enter arr[1][1][1]: 8
Output:
Elements of the Array:
1 2
3 4
5 6
7 8
Use Cases of Multi-Dimensional Arrays
There are some common heard use applications of Multi-Dimensional Arrays:
- Matrices in Mathematics: Representing grids or tables for mathematical operations.
- Game Development: Storing board game states, such as chess or tic-tac-toe.
- Data Storage: Representing tabular data for processing and visualization.
- Image Processing: Representing pixel data in two-dimensional arrays.