Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are two-dimensional arrays in C#?
A two-dimensional array in C# is an array of arrays, where elements are arranged in rows and columns like a table or matrix. It stores data in a grid format with two indices: one for the row and one for the column.
Two-dimensional arrays are useful for representing tabular data, matrices, game boards, or any data structure that requires a grid-like organization.
Syntax
Following is the syntax for declaring a two-dimensional array −
datatype[,] arrayName = new datatype[rows, columns];
Following is the syntax for initializing a two-dimensional array with values −
int[,] arrayName = new int[2,3] {
{1, 2, 3},
{4, 5, 6}
};
Using Two-Dimensional Arrays
Example 1: Basic Declaration and Access
This example shows how to declare, initialize, and access elements in a two-dimensional array −
using System;
class Program {
static void Main(string[] args) {
// Declare and initialize a 2D array
int[,] numbers = new int[2, 3] {
{1, 2, 3},
{4, 5, 6}
};
// Access and display individual elements
Console.WriteLine("Element at [0,0]: " + numbers[0,0]);
Console.WriteLine("Element at [1,2]: " + numbers[1,2]);
// Get dimensions
Console.WriteLine("Rows: " + numbers.GetLength(0));
Console.WriteLine("Columns: " + numbers.GetLength(1));
}
}
The output of the above code is −
Element at [0,0]: 1 Element at [1,2]: 6 Rows: 2 Columns: 3
Traversing Two-Dimensional Arrays
Example 2: Using Nested Loops
This example demonstrates how to traverse through all elements of a two-dimensional array using nested loops −
using System;
class Program {
static void Main(string[] args) {
// Create a 3x2 array
int[,] matrix = new int[3, 2] {{0,0}, {1,2}, {2,4}};
Console.WriteLine("Matrix elements:");
// Traverse using nested for loops
for (int i = 0; i < matrix.GetLength(0); i++) {
for (int j = 0; j < matrix.GetLength(1); j++) {
Console.WriteLine("matrix[{0},{1}] = {2}", i, j, matrix[i,j]);
}
}
}
}
The output of the above code is −
Matrix elements: matrix[0,0] = 0 matrix[0,1] = 0 matrix[1,0] = 1 matrix[1,1] = 2 matrix[2,0] = 2 matrix[2,1] = 4
Example 3: Matrix Operations
This example shows practical usage of two-dimensional arrays for matrix operations −
using System;
class Program {
static void Main(string[] args) {
// Create two 2x2 matrices
int[,] matrixA = {{1, 2}, {3, 4}};
int[,] matrixB = {{5, 6}, {7, 8}};
int[,] result = new int[2, 2];
// Add matrices
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
result[i, j] = matrixA[i, j] + matrixB[i, j];
}
}
// Display result
Console.WriteLine("Matrix Addition Result:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Console.Write(result[i, j] + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Matrix Addition Result: 6 8 10 12
Common Use Cases
Mathematical matrices − For linear algebra operations and calculations
Game boards − Chess, tic-tac-toe, or other grid-based games
Image processing − Storing pixel data in rows and columns
Tabular data − Representing spreadsheet-like data structures
Conclusion
Two-dimensional arrays in C# provide an efficient way to store and manipulate grid-like data structures. They use row and column indices for element access and are ideal for mathematical computations, game development, and any scenario requiring tabular data organization.
