In C#, an array is a collection of elements of the same data type stored in contiguous memory locations. C# supports single-dimensional, multi-dimensional, and jagged arrays.
Here is an example of a single-dimensional array in C#:
// declare and initialize an array of integers
int[] numbers = { 2, 4, 6, 8, 10 };
// access an element in the array using its index
int firstNumber = numbers[0]; // firstNumber will be 2
int thirdNumber = numbers[2]; // thirdNumber will be 6
// change the value of an element in the array
numbers[1] = 5; // the second element in the array will be 5 instead of 4
// loop through the array using a for loop
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
// loop through the array using a foreach loop
foreach (int number in numbers)
{
Console.WriteLine(number);
}
In this example, we declare and initialize an array of integers using an array initializer. We access elements in the array using their index, change the value of an element, and loop through the array using both a for loop and a foreach loop. Note that the Length property is used to get the number of elements in the array.
Example of 2D array:
// declare and initialize a 2D array of integers
int[,] matrix = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
// access an element in the array using its indices
int firstElement = matrix[0, 0]; // firstElement will be 1
int lastElement = matrix[2, 1]; // lastElement will be 6
// change the value of an element in the array
matrix[1, 0] = 7; // the second row, first column element will be 7 instead of 3
// loop through the array using nested for loops
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();
}
In this example, we declare and initialize a 2D array of integers using an array initializer. We access elements in the array using their indices, change the value of an element, and loop through the array using nested for loops. Note that the GetLength() method is used to get the number of elements in each dimension of the array.
A jagged array (also known as an array-of-arrays) is an array of arrays, where each element of the main array is itself an array. Unlike a multi-dimensional array, the sub-arrays in a jagged array can have different lengths.
Here is an example of a jagged array in C#:
// declare and initialize a jagged array of integers
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9 };
// access an element in the array using its indices
int firstElement = jaggedArray[0][0]; // firstElement will be 1
int lastElement = jaggedArray[2][3]; // lastElement will be 9
// change the value of an element in the array
jaggedArray[1][1] = 0; // the second row, second column element will be 0 instead of 5
// loop through the array using nested for loops
for (int i = 0; i < jaggedArray.Length; i++)
{
for (int j = 0; j < jaggedArray[i].Length; j++)
{
Console.Write(jaggedArray[i][j] + " ");
}
Console.WriteLine();
}
In this example, we declare and initialize a jagged array of integers using separate initialization statements for each sub-array. We access elements in the array using their indices, change the value of an element, and loop through the array using nested for loops. Note that the length of each sub-array can be different.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.