Introduction
Multi-dimensional arrays in C# are arrays of arrays, allowing you to store data in a tabular or matrix form. The most commonly used multi-dimensional array is the two-dimensional array, but C# also supports arrays with more dimensions.
Types of Multi-Dimensional Arrays
- Two-Dimensional Arrays
- Three-Dimensional Arrays
- Arrays with Higher Dimensions
1. Two-Dimensional Arrays
A two-dimensional array, also known as a matrix or a table, is an array where data is stored in rows and columns.
Syntax
dataType[,] arrayName = new dataType[rows, columns];
Example
using System;
namespace MultiDimensionalArrayExample
{
class Program
{
static void Main(string[] args)
{
// Declare and initialize a 2D array
int[,] matrix = new int[3, 3]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
// Access and display array elements
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
// Modify an array element
matrix[1, 1] = 10;
Console.WriteLine($"Modified element at [1,1]: {matrix[1, 1]}");
}
}
}
Output
1 2 3
4 5 6
7 8 9
Modified element at [1,1]: 10
2. Three-Dimensional Arrays
A three-dimensional array can be visualized as an array of arrays of arrays, extending the concept of 2D arrays into another dimension.
Syntax
dataType[,,] arrayName = new dataType[dimension1, dimension2, dimension3];
Example
using System;
namespace ThreeDimensionalArrayExample
{
class Program
{
static void Main(string[] args)
{
// Declare and initialize a 3D array
int[,,] threeDArray = new int[2, 2, 2]
{
{
{ 1, 2 },
{ 3, 4 }
},
{
{ 5, 6 },
{ 7, 8 }
}
};
// Access and display array elements
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{
Console.Write(threeDArray[i, j, k] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
// Modify an array element
threeDArray[1, 1, 1] = 10;
Console.WriteLine($"Modified element at [1,1,1]: {threeDArray[1, 1, 1]}");
}
}
}
Output
1 2
3 4
5 6
7 8
Modified element at [1,1,1]: 10
Accessing Elements
To access elements in multi-dimensional arrays, you use a separate index for each dimension.
Example
int[,] matrix = new int[3, 3]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
// Accessing an element
int value = matrix[1, 1]; // 5
Iterating Through Elements
You can use nested loops to iterate through elements of multi-dimensional arrays.
Example
using System;
namespace MultiDimensionalArrayIteration
{
class Program
{
static void Main(string[] args)
{
int[,] matrix = new int[3, 3]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
// Iterating through the array
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
Output
1 2 3
4 5 6
7 8 9
Practical Example
Let’s create a practical example where we use a two-dimensional array to store and display a simple multiplication table.
using System;
namespace MultiplicationTableExample
{
class Program
{
static void Main(string[] args)
{
int[,] multiplicationTable = new int[10, 10];
// Populate the multiplication table
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
multiplicationTable[i, j] = (i + 1) * (j + 1);
}
}
// Display the multiplication table
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Console.Write(multiplicationTable[i, j] + "\t");
}
Console.WriteLine();
}
}
}
}
Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Conclusion
Multi-dimensional arrays in C# provide a powerful way to manage and organize complex data structures. They allow you to store data in a tabular or matrix form, making it easier to handle related data. By understanding how to declare, initialize, and manipulate multi-dimensional arrays, you can effectively manage more complex collections of data in your applications.