Arrays in C#
Arrays are used to store multiple values of the same data type in a single variable. In C#, arrays can be declared and initialized in several ways. Here's an example code that demonstrates how to declare and initialize an array:
int[] numbers = new int[5]; // declares an array of 5 integers
numbers[0] = 10; // assigns 10 to the first element of the array
numbers[1] = 20; // assigns 20 to the second element of the array
numbers[2] = 30; // assigns 30 to the third element of the array
numbers[3] = 40; // assigns 40 to the fourth element of the array
numbers[4] = 50; // assigns 50 to the fifth element of the array
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]); // prints all the elements of the array
}
No comments:
Post a Comment