Arrays in C# are a fundamental data structure used to store a collection of elements of the same data type. They provide a convenient way to organize and manipulate data.
There are two main types of arrays in C#:
1. Single-Dimensional Arrays ๐ A single-dimensional array stores elements in a linear sequence.
int[] numbers = new int[5]; // Declares an array of 5 integers
// Initializing the elements individually. index starts from "0"
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
string[] fruits = { "Apple", "Banana", "Orange" }; // Initializes an array with values
// Accessing the second element of the 'numbers' array
int secondNumber = numbers[1];
// Accessing the last element of the 'fruits' array
string lastFruit = fruits[fruits.Length - 1];
//Iterating Through Arrays using FOR loop
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
//Iterating Through Arrays using FOREACH loop
foreach (var fruit in fruits)
{
Console.WriteLine(fruit);
}
Simple Example:
int[] scores = { 90, 85, 78, 92, 88 }; // Declare and initialize an array of scores
// Calculate the average score
int sum = 0; // Initialize a variable to store the sum of scores
foreach (int score in scores) // Iterate through each score in the array
{
sum += score; // Add the current score to the sum
}
double average = (double)sum / scores.Length; // Calculate the average by dividing the sum
// by the number of scores, casting the sum to double to ensure floating-point division
Console.WriteLine("Average score: " + average); // Print the calculated average to the console
2. Multidimensional Arrays ๐ข Multidimensional arrays store elements in a tabular format, similar to a matrix.
int[,] matrix = new int[2, 3]; // Declares a 2x3 matrix
// Initializing the matrix
matrix[0, 0] = 1;
matrix[0, 1] = 2;
matrix[0, 2] = 3;
matrix[1, 0] = 4;
matrix[1, 1] = 5;
matrix[1, 2] = 6;
//Similar to matrix: int[number of rows, number of columns], total elements = row x column.
int[,] matrix2 = { { 1, 2, 3 }, { 4, 5, 6 } }; // Initializes a 2x3 matrix
Console.WriteLine(matrix[1, 2]); // Accesses the element at row 1, column 2
//Iterating Through Multidimensional Arrays:
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();
}
Simple Example:
int[,] multiplicationTable = new int[10, 10];
// Populate the multiplication table
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
multiplicationTable[i, j] = (i + 1) * (j + 1);
}
}
// Print the multiplication table
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Console.Write(multiplicationTable[i, j] + "\t");
}
Console.WriteLine();
}
Key Points to Remember:
Array indices start from 0.
Array sizes must be specified at declaration or initialization.
Array elements are stored in contiguous memory locations.
Arrays are reference types, so assigning one array to another creates a reference to the same memory location.
Multidimensional arrays can have any number of dimensions.
Remember that array indices are zero-based, and accessing an index that is out of bounds will result in an
IndexOutOfRangeException
By understanding and effectively using arrays, you can create efficient and structured data representations in your C# programs.