In programming, arrays are fundamental data structures for storing collections of data. While single-dimensional arrays are a sequence of elements in a single line, multi-dimensional arrays allow for the organization of data in multiple rows and columns, forming a grid or matrix-like structure. C# supports multi-dimensional arrays, making them useful in many scenarios where data is more complex than a simple list.
A multi-dimensional array in C# is an array with more than one dimension, such as a 2D or 3D array. Multi-dimensional arrays are ideal for applications that require a table of data, like spreadsheets, or more complex grids, like chessboards.
Multi-dimensional arrays are beneficial when:
A two-dimensional array in C# resembles a table with rows and columns, where each element is accessed by specifying two indices (row and column).
A three-dimensional array can be visualized as a collection of 2D arrays stacked on top of each other, useful for applications that need to store data in three dimensions.
To declare and initialize a two-dimensional array:
// Declare a 2D array
int[,] matrix = new int[3, 3]; // 3 rows, 3 columns
// Initialize with values
int[,] grid = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
To declare and initialize a three-dimensional array:
// Declare a 3D array
int[,,] cube = new int[3, 3, 3]; // 3 layers, 3 rows, 3 columns
// Initialize with values
int[,,] cubeValues = {
{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} },
{ {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
{ {19, 20, 21}, {22, 23, 24}, {25, 26, 27} }
};
In both examples:
matrix
is a 3x3 table with 9 elements.cube
is a 3x3x3 cube with 27 elements.Elements in a multi-dimensional array are accessed using comma-separated indices.
int[,] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing element at row 1, column 2
Console.WriteLine(matrix[1, 2]); // Output: 6
// Change the value at row 0, column 1
matrix[0, 1] = 10;
Console.WriteLine(matrix[0, 1]); // Output: 10
To iterate through all elements in a 2D array, use nested loops:
for (int i = 0; i < matrix.GetLength(0); i++) // Rows
{
for (int j = 0; j < matrix.GetLength(1); j++) // Columns
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
Consider a scenario where a retail company wants to store the monthly sales data for each product category over the span of a year. A two-dimensional array can be used to represent this data, where each row corresponds to a category and each column to a month.
Store and calculate:
Using a two-dimensional array to store the sales data, we can easily calculate the totals and averages.
class SalesAnalysis
{
static void Main(string[] args)
{
// Array representing sales data for each category (rows) for each month (columns)
int[,] sales = {
{500, 700, 800, 650, 700, 750, 600, 680, 620, 710, 790, 780}, // Category 1
{300, 400, 500, 450, 480, 470, 500, 520, 530, 490, 510, 550}, // Category 2
{250, 300, 320, 310, 290, 300, 330, 340, 350, 340, 370, 400} // Category 3
};
// Calculating total yearly sales for each category
for (int i = 0; i < sales.GetLength(0); i++)
{
int yearlyTotal = 0;
for (int j = 0; j < sales.GetLength(1); j++)
{
yearlyTotal += sales[i, j];
}
Console.WriteLine($"Total sales for Category {i + 1}: {yearlyTotal}");
}
// Calculating average monthly sales across all categories
int grandTotal = 0;
int totalMonths = sales.GetLength(0) * sales.GetLength(1);
foreach (int sale in sales)
{
grandTotal += sale;
}
double averageMonthlySales = (double)grandTotal / totalMonths;
Console.WriteLine($"Average monthly sales across all categories: {averageMonthlySales}");
}
}
sales
array to represent each category’s monthly sales data.Total sales for Category 1: 8550
Total sales for Category 2: 6000
Total sales for Category 3: 3880
Average monthly sales across all categories: 506.67
GetLength(dim)
to retrieve the size of a particular dimension.Multi-dimensional arrays in C# enable developers to manage data in more complex structures, making them ideal for applications requiring tables, grids, or 3D representations. This guide covered two-dimensional and three-dimensional arrays, how to declare and initialize them, and common operations like accessing, modifying, and iterating through elements. With practical applications ranging from data tables to real-world sales analysis, multi-dimensional arrays provide an efficient solution for organizing and managing structured data.