C# 数组
变量用于存储字面值,而数组用于存储多个字面值。
数组是一种数据结构,用于存储固定数量的相同数据类型的字面值(元素)。数组元素在内存中是连续存储的。
在 C# 中,数组可以分为三种类型:一维数组、多维数组和交错数组。本文将介绍一维数组。
下图展示了数组的表示。

数组的声明和初始化
可以通过指定元素类型并使用方括号来声明数组。
int[] evenNums; // integer array
string[] cities; // string array
以下语句在单行中声明数组并添加值。
int[] evenNums = new int[5]{ 2, 4, 6, 8, 10 };
string[] cities = new string[3]{ "Mumbai", "London", "New York" };
上面,`evenNums` 数组最多可以存储五个整数。方括号 `new int[5]` 中的数字 5 指定了数组的大小。同样,`cities` 数组的大小是三。数组元素在花括号内以逗号分隔的列表形式添加。.
可以使用 `var` 声明数组类型变量,无需方括号。
var evenNums = new int[]{ 2, 4, 6, 8, 10};
var cities = new string[]{ "Mumbai", "London", "New York" };
如果在声明时添加数组元素,则大小是可选的。编译器将根据花括号内的元素数量推断其大小,如下所示。
int[] evenNums = { 2, 4, 6, 8, 10};
string[] cities = { "Mumbai", "London", "New York" }
以下示例演示了无效的数组声明。
//must specify the size
int[] evenNums = new int[];
//number of elements must be equal to the specified size
int[] evenNums = new int[5] { 2, 4 };
//cannot use var with array initializer
var evenNums = { 2, 4, 6, 8, 10};
延迟初始化
无需在单个语句中声明和初始化数组。您可以先声明数组,然后使用 `new` 运算符稍后对其进行初始化。
int[] evenNums;
evenNums = new int[5];
// or
evenNums = new int[]{ 2, 4, 6, 8, 10 };
访问数组元素
可以使用索引访问数组元素。索引是与每个数组元素相关联的数字,从索引 0 开始,到数组大小 - 1 结束。
以下示例使用索引添加/更新和检索数组元素。
int[] evenNums = new int[5];
evenNums[0] = 2;
evenNums[1] = 4;
//evenNums[6] = 12; //Throws run-time exception IndexOutOfRange
Console.WriteLine(evenNums[0]); //prints 2
Console.WriteLine(evenNums[1]); //prints 4
请注意,尝试添加超出其指定大小的元素将导致 `IndexOutOfRangeException`。
使用 for 循环访问数组
使用 `for` 循环访问数组元素。在 `for` 循环的条件表达式中使用数组的 `Length` 属性。
int[] evenNums = { 2, 4, 6, 8, 10 };
for(int i = 0; i < evenNums.Length; i++)
Console.WriteLine(evenNums[i]);
for(int i = 0; i < evenNums.Length; i++)
evenNums[i] = evenNums[i] + 10; // update the value of each element by 10
使用 foreach 循环访问数组
使用 `foreach` 循环无需使用索引即可读取数组元素的值。
int[] evenNums = { 2, 4, 6, 8, 10};
string[] cities = { "Mumbai", "London", "New York" };
foreach(var item in evenNums)
Console.WriteLine(item);
foreach(var city in cities)
Console.WriteLine(city);
LINQ 方法
C# 中的所有数组都派生自抽象基类 `System.Array`。
`Array` 类实现了 `IEnumerable` 接口,因此您可以使用 LINQ 扩展方法,例如 `Max()`、`Min()`、`Sum()`、`Reverse()` 等。在此处查看所有扩展方法的列表。
int[] nums = new int[5]{ 10, 15, 16, 8, 6 };
nums.Max(); // returns 16
nums.Min(); // returns 6
nums.Sum(); // returns 55
nums.Average(); // returns 55
`System.Array` 类还包含用于创建、操作、搜索和排序数组的方法。在此处查看所有 `Array` 方法的列表。
int[] nums = new int[5]{ 10, 15, 16, 8, 6 };
Array.Sort(nums); // sorts array
Array.Reverse(nums); // sorts array in descending order
Array.ForEach(nums, n => Console.WriteLine(n)); // iterates array
Array.BinarySearch(nums, 5);// binary search
将数组作为参数传递
数组可以作为参数传递给方法参数。数组是引用类型,因此方法可以更改数组元素的值。
public static void Main(){
int[] nums = { 1, 2, 3, 4, 5 };
UpdateArray(nums);
foreach(var item in nums)
Console.WriteLine(item);
}
public static void UpdateArray(int[] arr)
{
for(int i = 0; i < arr.Length; i++)
arr[i] = arr[i] + 10;
}
接下来了解多维数组和交错数组。
- 如何在 C# 中搜索数组?
- 如何在 C# 中对数组进行排序?
- 如何在 C# 中按特定属性对对象数组进行排序?
- 如何在 C# 中从数组获取逗号分隔的字符串?
- 如何在 C# 中从数组中删除重复值?
- 如何在 C# 中合并两个数组而不重复值?
- 如何在 C# 中计算数组中的特定元素?
- 数组与 ArrayList 的区别