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
How to declare a two-dimensional array in C#
A two-dimensional array in C# is a data structure that stores elements in a grid-like format with rows and columns. It can be thought of as an array of arrays, where each element is accessed using two indices.
Syntax
Following is the syntax for declaring a two-dimensional array −
datatype[,] arrayName;
Following is the syntax for initializing a two-dimensional array −
datatype[,] arrayName = new datatype[rows, columns];
You can also declare and initialize in one statement −
datatype[,] arrayName = new datatype[rows, columns] {
{value1, value2, ...},
{value3, value4, ...},
...
};
Declaration and Initialization
Basic Declaration
You can declare a two-dimensional array without initialization −
using System;
class Program {
static void Main() {
int[,] numbers = new int[2, 3];
// Assigning values
numbers[0, 0] = 10;
numbers[0, 1] = 20;
numbers[0, 2] = 30;
numbers[1, 0] = 40;
numbers[1, 1] = 50;
numbers[1, 2] = 60;
Console.WriteLine("Element at [0,1]: " + numbers[0, 1]);
Console.WriteLine("Element at [1,2]: " + numbers[1, 2]);
}
}
The output of the above code is −
Element at [0,1]: 20 Element at [1,2]: 60
Declaration with Initialization
You can initialize a two-dimensional array at the time of declaration −
using System;
class Program {
static void Main() {
int[,] matrix = new int[3, 3] {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Display the matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
1 2 3 4 5 6 7 8 9
Using GetLength() Method
The GetLength() method returns the number of elements in a specified dimension. This is useful when you don't know the array size beforehand −
using System;
class Program {
static void Main() {
int[,] data = new int[3, 4] {
{10, 20, 30, 40},
{50, 60, 70, 80},
{90, 100, 110, 120}
};
int rows = data.GetLength(0); // Number of rows
int cols = data.GetLength(1); // Number of columns
Console.WriteLine("Array dimensions: " + rows + " x " + cols);
// Print all elements
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Console.Write("data[" + i + "," + j + "] = " + data[i, j] + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Array dimensions: 3 x 4 data[0,0] = 10 data[0,1] = 20 data[0,2] = 30 data[0,3] = 40 data[1,0] = 50 data[1,1] = 60 data[1,2] = 70 data[1,3] = 80 data[2,0] = 90 data[2,1] = 100 data[2,2] = 110 data[2,3] = 120
Conclusion
Two-dimensional arrays in C# provide an efficient way to store and manipulate data in a tabular format. They are declared using the [,] syntax and accessed using two indices representing row and column positions. Use the GetLength() method to determine array dimensions dynamically.
