What does “array” mean in C#?

In C#, an array is a data structure used to store multiple elements of the same type. It is a fixed-length container that can store multiple elements consecutively in memory.

The size of an array is determined at the time of creation and the type of elements must be specified. The elements can be of any valid C# data type, such as integers, floats, characters, strings, etc.

Each element in an array has a unique index that starts from 0 and increases sequentially. Using these indexes, one can access and modify elements in the array. Arrays offer a convenient way to organize and manipulate large amounts of data.

Here is an example of creating and using an array:

int[] numbers = new int[5]; // 创建一个包含5个整数的数组

numbers[0] = 10; // 设置第一个元素的值为10
numbers[1] = 20; // 设置第二个元素的值为20

Console.WriteLine(numbers[0]); // 输出第一个元素的值
Console.WriteLine(numbers[1]); // 输出第二个元素的值

Output result:

10
20

When accessing array elements by index, it is important to note that the index cannot exceed the boundaries of the array, otherwise it will trigger an “IndexOutOfRangeException” exception.

Arrays also provide some common properties and methods, such as the Length property to get the length of the array and the Sort method to sort the array.

bannerAds