Arrays

Arrays in C# are a fundamental data structure that allow you to store multiple values of the same type in a single variable. They are commonly used to manage collections of data in an organized manner. Understanding arrays is crucial for any C# programmer as they provide the foundation for more complex data structures and algorithms.

What is an Array?

An array is a fixed-size, ordered collection of elements of the same type. Each element can be accessed by its index, with the first element having an index of 0. Arrays are useful when you need to store and manipulate a collection of data items, such as numbers, strings, or objects.

Declaring and Initializing Arrays

In C#, you can declare and initialize arrays in several ways. Here are some common methods:

1. Declaring an Array:

int[] numbers;

2. Initializing an Array:

You can initialize an array at the time of declaration or later in the code.
At Declaration:
int[] numbers = new int[6]; // Array of integers with 6 elements
With Values:
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6 };
Implicitly:
int[] numbers = { 1, 2, 3, 4, 5, 6 };

Accessing Array Elements

You can access array elements using their index. The index is zero-based, meaning the first element is at index 0, the second at index 1, and so on.
int[] numbers = { 1, 2, 3, 4, 5, 6 };
Console.WriteLine(numbers[0]); // Outputs: 1
Console.WriteLine(numbers[4]); // Outputs: 5

Modifying Array Elements

You can modify the elements of an array by accessing them using their index and assigning new values
int[] numbers = { 1, 2, 3, 4, 5, 6 };
numbers[0] = 10;
numbers[4] = 50;
Console.WriteLine(numbers[0]); // Outputs: 10
Console.WriteLine(numbers[4]); // Outputs: 50

Iterating Over Arrays

You can iterate over the elements of an array using loops such as for or foreach.
Using for Loop:
int[] numbers = { 1, 2, 3, 4, 5, 6 };
for (int i = 0; i < numbers.Length; i++)
{
  Console.WriteLine(numbers[i]);
}

Output

1
2
3
4
5
6
Using foreach Loop:
int[] numbers = { 1, 2, 3, 4, 5, 6 };
foreach (int number in numbers)
{
  Console.WriteLine(number);
}

Output

1
2
3
4
5
6

Multi-Dimensional Arrays

C# supports multi-dimensional arrays, such as 2D arrays (matrices) and 3D arrays. These are useful for representing grids, tables, and other complex data structures.
Declaring and Initializing a 2D Array:
int[,] matrix = new int[3, 3]; // 3x3 matrix
int[,] predefinedMatrix = {
  { 1, 2, 3 },
  { 4, 5, 6 },
  { 7, 8, 9 }
};
Accessing Elements in a 2D Array:
Console.WriteLine(predefinedMatrix[0, 0]); // Outputs: 1
Console.WriteLine(predefinedMatrix[2, 2]); // Outputs: 9
Iterating Over a 2D Array:
int[,] matrix = {
  { 1, 2, 3 },
  { 4, 5, 6 },
  { 7, 8, 9 }
};
for (int i = 0; i < matrix.GetLength(0); i++)
{
  for (int j = 0; j < matrix.GetLength(1); j++)
  {
      Console.Write(matrix[i, j] + " ");
  }
  Console.WriteLine();
}

Array Methods

C# provides several built-in methods for working with arrays, including:
Sorting Arrays
int[] numbers = { 5, 3, 1, 4, 2 };
Array.Sort(numbers);
foreach (int number in numbers)
{
  Console.WriteLine(number); // Outputs: 1 2 3 4 5
}
Searching Arrays
int[] numbers = { 1, 2, 3, 4, 5 };
int index = Array.IndexOf(numbers, 3);
Console.WriteLine(index); // Outputs: 2
Copying Arrays
int[] source = { 1, 2, 3, 4, 5 };
int[] destination = new int[5];
Array.Copy(source, destination, source.Length);
foreach (int number in destination)
{
  Console.WriteLine(number); // Outputs: 1 2 3 4 5
}
Post a comment

Leave a Comment

Scroll to Top