Array in C# Programming
In C#, an array is a collection of elements of the same data type, which is a fixed-size container that holds a specific number of elements that are stored contiguously in memory. Arrays can be of any data type such as integer, float, double, character, etc., and they are used to store and manipulate collections of data. In C#, arrays are declared using square brackets [] and initialized with specific sizes and values. The elements in an array are accessed using an index, which is a numeric value that represents the position of an element in the array.
Here is an example of creating and using an array in C#:
int[] EvenNumbers = new int[5];
numbers[0] = 2;
numbers[1] = 4;
numbers[2] = 6;
numbers[3] = 8;
numbers[4] = 10;
for (int i = 0; i < EvenNumbers.Length; i++)
{
Console.WriteLine(EvenNumbers[i]);
}
In this example, an array of integers named EvenNumbers
is declared and initialized with a length of 5 using the new
keyword. The values of the array elements are then set individually using the index notation (numbers[2]
, numbers[4]
, etc.). Finally, a for
loop is used to iterate over the array and print each element to the console using the Console.WriteLine
method. The Length
property of the array is used to determine the number of iterations for the loop.