数组是相似类型数据的集合。例如,
假设我们需要记录 5 名学生的年龄。与其创建 5 个单独的变量,不如直接创建一个数组

1. C# 数组声明
在 C# 中,声明数组的方法如下。
datatype[] arrayName;
这里,
dataType
- 数据类型,如int
、string
、char
等arrayName
- 这是一个标识符
让我们看一个例子,
int[] age;
在这里,我们创建了一个名为 age 的数组。它可以存储 int
类型的数据。
但它能存储多少个元素呢?
要定义数组可以容纳的元素数量,我们必须在 C# 中为数组分配内存。例如,
// declare an array
int[] age;
// allocate memory for array
age = new int[5];
这里,new int[5]
表示该数组可以存储 5 个元素。我们也可以说数组的大小/长度是 5。
注意:我们也可以在一行中声明和分配数组内存。例如,
int[] age = new int[5];
2. C# 中的数组初始化
在 C# 中,我们可以在声明时初始化数组。例如,
int [] numbers = {1, 2, 3, 4, 5};
在这里,我们创建了一个名为 numbers 的数组,并在花括号内用值 **1**、**2**、**3**、**4** 和 **5** 对其进行了初始化。
请注意,我们没有提供数组的大小。在这种情况下,C# 会通过计算数组中的元素数量(即 5)自动指定大小。
在数组中,我们使用**索引号**来确定每个数组元素的位置。我们可以使用索引号在 C# 中初始化数组。例如,
// declare an array
int[] age = new int[5];
//initializing array
age[0] = 12;
age[1] = 4;
age[2] = 5;
...

注意:
- 数组索引始终从 0 开始。也就是说,数组的第一个元素位于索引 0 处。
- 如果数组的大小是 5,那么最后一个元素的索引将是 4(5 - 1)。
3. 访问数组元素
我们可以使用数组的索引来访问数组中的元素。例如,
// access element at index 2
array[2];
// access element at index 4
array[4];
这里,
array[2]
- 访问第 3 个元素array[4]
- 访问第 5 个元素
示例:C# 数组
using System;
namespace AccessArray {
class Program {
static void Main(string[] args) {
// create an array
int[] numbers = {1, 2, 3};
//access first element
Console.WriteLine("Element in first index : " + numbers[0]);
//access second element
Console.WriteLine("Element in second index : " + numbers[1]);
//access third element
Console.WriteLine("Element in third index : " + numbers[2]);
Console.ReadLine();
}
}
}
输出
Element in first index : 1 Element in second index : 2 Element in third index : 3
在上面的示例中,我们创建了一个名为 numbers 的数组,其元素为 **1, 2, 3**。这里,我们使用**索引号**来访问数组中的元素。
numbers[0]
- 访问第一个元素,**1**numbers[1]
- 访问第二个元素,**2**numbers[3]
- 访问第三个元素,**3**
4. 更改数组元素
我们也可以更改数组中的元素。要更改元素,只需为该特定索引分配一个新值即可。例如,
using System;
namespace ChangeArray {
class Program {
static void Main(string[] args) {
// create an array
int[] numbers = {1, 2, 3};
Console.WriteLine("Old Value at index 0: " + numbers[0]);
// change the value at index 0
numbers[0] = 11;
//print new value
Console.WriteLine("New Value at index 0: " + numbers[0]);
Console.ReadLine();
}
}
}
输出
Old Value at index 0: 1 New Value at index 0: 11
在上面的示例中,索引 0 的初始值为 1。请注意这一行,
//change the value at index 0
numbers[0] = 11;
这里,我们将索引 0 的新值分配为 **11**。现在,索引 0 的值已从 **1** 更改为 **11**。
5. 使用循环迭代 C# 数组
在 C# 中,我们可以使用循环来遍历数组中的每个元素。例如,
示例:使用 for 循环
using System;
namespace AccessArrayFor {
class Program {
static void Main(string[] args) {
int[] numbers = { 1, 2, 3};
for(int i=0; i < numbers.Length; i++) {
Console.WriteLine("Element in index " + i + ": " + numbers[i]);
}
Console.ReadLine();
}
}
}
输出
Element in index 0: 1 Element in index 1: 2 Element in index 2: 3
在上面的示例中,我们使用 for 循环来迭代数组 numbers 的元素。请注意这一行,
numbers.Length
这里,数组的 Length
属性给出数组的大小。
我们也可以使用 foreach 循环来迭代数组中的元素。例如,
示例:使用 foreach 循环
using System;
namespace AccessArrayForeach {
class Program {
static void Main(string[] args) {
int[] numbers = {1, 2, 3};
Console.WriteLine("Array Elements: ");
foreach(int num in numbers) {
Console.WriteLine(num);
}
Console.ReadLine();
}
}
}
输出
Array Elements: 1 2 3
6. 使用 System.Linq 进行 C# 数组操作
在 C# 中,我们有 System.Linq
命名空间,它提供了各种方法来对数组执行各种操作。例如,
示例:查找最小和最大元素
using System;
// provides us various methods to use in an array
using System.Linq;
namespace ArrayMinMax {
class Program {
static void Main(string[] args) {
int[] numbers = {51, 1, 3, 4, 98};
// get the minimum element
Console.WriteLine("Smallest Element: " + numbers.Min());
// Max() returns the largest number in array
Console.WriteLine("Largest Element: " + numbers.Max());
Console.ReadLine();
}
}
}
输出
Smallest Element: 1 Largest Element: 98
在上面的例子中:
numbers.Min()
- 返回数组中的最小值,**1**numbers.Max()
- 返回数组中的最大值,**98**
示例:查找数组的平均值
using System;
// provides us various methods to use in an array
using System.Linq;
namespace ArrayFunction {
class Program {
static void Main(string[] args) {
int[] numbers = {30, 31, 94, 86, 55};
// get the sum of all array elements
float sum = numbers.Sum();
// get the total number of elements present in the array
int count = numbers.Count();
float average = sum/count;
Console.WriteLine("Average : " + average);
// compute the average
Console.WriteLine("Average using Average() : " + numbers.Average());
Console.ReadLine();
}
}
}
输出
Average : 59.2 Average using Average() : 59.2
在上面的示例中,我们使用了
numbers.Sum()
获取数组所有元素的总和numbers.Count()
获取数组中存在的元素总数
然后我们用总和除以数量来得到平均值。
float average = sum / count;
这里,我们还使用了 System.Linq
命名空间的方法 numbers.Average()
来直接获取平均值。
注意:在使用 Min()
、Max()
、Sum()
、Count()
和 Average()
方法时,必须使用 System.Linq
命名空间。