Single-Dimensional, Two-Dimensional, and Multidimensional Arrays in C#

Introduction

Arrays are fundamental data structures in programming languages, including C#. They allow you to store and manipulate collections of data efficiently. In C#, arrays come in different flavors, each serving specific purposes. In this article, we'll explore single-dimensional, two-dimensional, and multidimensional arrays in C#, understand their differences, and learn how to work with them effectively.

Single Array Declaration

Arrays are used to store multiple values in a single variable instead of declaring separate variables for each value.

To declare an array, define the variable type with square brackets:

//without values
string[] cars;

//with values
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

int[] myNum = {10, 20, 30, 40};

Access the Elements of an Array

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Console.WriteLine(cars[0]);
// Outputs Volvo

Change an Array Element

To change the value of a specific element, refer to the index number:

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
Console.WriteLine(cars[0]);
// Now outputs Opel instead of Volvo

Two-Dimensional Arrays Declaration

To create a 2D array, add each array within its own set of curly braces and insert a comma (,) inside the square brackets:

int[,] numbers1 = new int[0, 0];

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };

Two- Dimensional Array in C#

Access the Elements of an Array

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };
Console.WriteLine(numbers[0, 2]);  // Outputs 2

Change an Array Element

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };
numbers[0, 0] = 5;  // Change value to 5
Console.WriteLine(numbers[0, 0]); // Outputs 5 instead of 1

Loop Through a 2D Array

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };

foreach (int i in numbers)
{
  Console.WriteLine(i+ " ");
}
// o/p: 1 4 2 3 6 8

Multidimensional Array Declaration

There are 3 ways to initialize a multidimensional array in C# while declaration:

int[,] arr = new int[3,3]= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };  

//We can omit the array size.
int[,] arr = new int[,]{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };  

//We can omit the new operator also.
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

Access & Change the Elements of an Array

// Getting values from the array
int valueAtRow1Column2 = arr[0, 1];  // Accessing the element at row 0, column 1 (indexing is zero-based)
Console.WriteLine("Value at row 0, column 1: " + valueAtRow1Column2); // Output: 2

// Setting values in the array
arr[1, 2] = 10; // Setting the value at row 1, column 2 to 10

Loop Through a 3D Array

for(int i=0;i<3;i++){  
            for(int j=0;j<3;j++){  
                Console.Write(arr[i,j]+" ");  
            }  
            Console.WriteLine();//new line at each row

Output

1 2 3
4 5 6
7 8 9

Conclusion

Arrays are powerful data structures in C#, allowing you to store and manipulate collections of data efficiently. Whether you're working with single-dimensional, two-dimensional, or multidimensional arrays, understanding how to declare, initialize, and work with them is essential for writing effective C# code. By mastering arrays, you'll be better equipped to handle a wide range of programming tasks and challenges.


Similar Articles