C# Multi-dimensional arrays


In C#, multi-dimensional arrays allow you to store data in more than one dimension. They are useful for representing tables or matrices where data is organized into rows and columns.

Types of Multi-Dimensional Arrays

  1. Two-Dimensional Arrays: Often used to represent a matrix or table of data.
  2. Three-Dimensional Arrays: Used to represent data in a 3D space (e.g., layers of a table).
  3. N-Dimensional Arrays: You can create arrays with even more dimensions, but it is uncommon to go beyond 3D.

1. Two-Dimensional Arrays

A two-dimensional array is like a table, with rows and columns.

Declaring a Two-Dimensional Array

dataType[,] arrayName;
  • dataType specifies the type of elements the array will store.
  • The [,] indicates that it's a two-dimensional array.

Initializing a Two-Dimensional Array

int[,] matrix = new int[3, 4]; // A 3x4 matrix (3 rows, 4 columns)

You can also initialize it with values at the time of declaration:

int[,] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };

Accessing Elements in a Two-Dimensional Array

Elements are accessed using two indices, one for the row and one for the column:

int value = matrix[0, 2]; // Access the element in the first row and third column Console.WriteLine(value); // Output: 3

Modifying Elements

You can also modify elements using their indices:

matrix[1, 1] = 99; // Change the value at row 2, column 2 to 99

Example of Two-Dimensional Array

using System; public class Program { public static void Main() { // Declare and initialize a 2D array int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Access and print specific elements Console.WriteLine("Element at [0, 2]: " + matrix[0, 2]); // Output: 3 // Modify an element matrix[1, 1] = 99; // Print the modified matrix for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { Console.Write(matrix[row, col] + "\t"); } Console.WriteLine(); } } }

2. Three-Dimensional Arrays

A three-dimensional array adds another layer, which can be visualized as multiple 2D tables stacked on top of each other.

Declaring a Three-Dimensional Array

dataType[,,] arrayName;

For example:

int[,,] array3D = new int[2, 3, 4]; // A 2x3x4 array

Accessing and Modifying Elements

You can access or modify elements using three indices:

int value = array3D[1, 2, 3]; // Access the element at the second table, third row, fourth column array3D[0, 1, 2] = 42; // Modify the value

3. Higher-Dimensional Arrays

C# also supports higher-dimensional arrays (4D, 5D, etc.), though they are rarely used. They follow the same basic principles, but their usage becomes more complex.

Multi-Dimensional Array Properties and Methods

  • Length: Returns the total number of elements across all dimensions.

    int totalElements = matrix.Length; // For a 3x3 matrix, this returns 9
  • GetLength(dimensionIndex): Returns the number of elements in a specific dimension.

    int rows = matrix.GetLength(0); // Number of rows int columns = matrix.GetLength(1); // Number of columns

Example of a Three-Dimensional Array

using System; public class Program { public static void Main() { // Declare and initialize a 3D array int[,,] array3D = { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }; // Access and print elements Console.WriteLine("Element at [0, 1, 2]: " + array3D[0, 1, 2]); // Output: 6 // Modify an element array3D[1, 0, 1] = 99; // Print the entire array for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 3; k++) { Console.Write(array3D[i, j, k] + "\t"); } Console.WriteLine(); } } } }

Summary

  • Multi-dimensional arrays store data in more than one dimension (2D, 3D, etc.).
  • Elements are accessed using multiple indices (one for each dimension).
  • You can declare, initialize, access, and modify multi-dimensional arrays just like single-dimensional arrays, but with multiple indices.
  • Two-dimensional arrays are the most common, often used to represent matrices or tables.