C# array

In C#, an array is a collection of elements of the same type that are stored in a contiguous block of memory. Each element in the array can be accessed using an index, which is an integer value that represents the position of the element in the array.

Here is an example of how to declare and initialize an array in C#:

int[] numbers = new int[5];
So‮:ecru‬www.theitroad.com

In this example, the numbers array is declared as an array of integers with a length of 5. The new keyword is used to create a new instance of the array, and the length of the array is specified in the square brackets.

Here is an example of how to initialize an array with values:

int[] numbers = new int[] { 1, 2, 3, 4, 5 };

In this example, the numbers array is initialized with the values 1, 2, 3, 4, and 5.

Arrays can also be multidimensional or jagged (an array of arrays). Here is an example of how to declare and initialize a two-dimensional array in C#:

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

In this example, the matrix array is declared as a two-dimensional array of integers with three rows and three columns. The values are initialized using an initializer list that contains three nested arrays.

To access an element in an array, you can use its index. The index of the first element in an array is 0, and the index of the last element is the length of the array minus one. Here is an example of how to access an element in an array:

int[] numbers = new int[] { 1, 2, 3, 4, 5 };
Console.WriteLine(numbers[2]); // Output: 3

In this example, the third element of the numbers array is accessed using the index 2, which corresponds to the value 3.