C# Multidimensional Arrays
Last Updated :
15 Jan, 2025
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:
C#
// 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();
}
}
}
OutputElements 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:
C#
// 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.
C#
// 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.
Similar Reads
C# Jagged Arrays A jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows
4 min read
C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read
Multidimensional Arrays in C - 2D and 3D Arrays A multi-dimensional array in C can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays include 2D arrays which grows in two dimensions, and 3D arrays which grows in three dimension
8 min read
How to Get Value of Multidimensional Array in C? Prerequisite: Array in C An array is a type of data structure where we can store multiple elements of similar data types. A multidimensional array can be termed an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order. Declaration
8 min read
One Dimensional Arrays in C In C, an array is a collection of elements of the same type stored in contiguous memory locations. This organization allows efficient access to elements using their index. Arrays can also be of different types depending upon the direction/dimension they can store the elements. It can be 1D, 2D, 3D,
5 min read