数组Array是 C# 中最常用的数据结构之一它可以存储多个相同类型的数据并使用**索引Index**来访问每个元素。例如一个班级有 50 名学生如果不用数组就需要定义 50 个变量string student1 张三; string student2 李四; string student3 王五;使用数组后只需要一个变量string[] students { 张三, 李四, 王五 };一、什么是数组数组是一组相同数据类型元素的集合。特点数据类型必须一致大小固定创建后不能改变长度连续存储使用索引访问元素索引从0开始例如int[] nums { 10, 20, 30, 40 };内存结构索引(Index) 0 1 2 3 ┌───┬───┬───┬───┐ │10 │20 │30 │40 │ └───┴───┴───┴───┘访问Console.WriteLine(nums[0]); //10 Console.WriteLine(nums[2]); //30二、声明数组语法数据类型[] 数组名;例如int[] numbers; string[] names; double[] prices; bool[] flags;这里只是声明没有分配内存。三、创建数组使用 newint[] numbers new int[5];表示长度5 索引 0 1 2 3 4默认值0 0 0 0 0输出Console.WriteLine(numbers[0]);结果0四、初始化数组方法一先创建再赋值int[] numbers new int[3]; numbers[0] 100; numbers[1] 200; numbers[2] 300;方法二创建时初始化int[] numbers new int[] { 100, 200, 300 };方法三最常用int[] numbers { 100, 200, 300 };或者int[] numbers { 100, 200, 300 };五、访问数组元素语法数组名[索引]例如string[] names { Tom, Jack, Lucy }; Console.WriteLine(names[0]); Console.WriteLine(names[1]); Console.WriteLine(names[2]);输出Tom Jack Lucy六、修改数组元素int[] score { 80, 90, 70 }; score[1] 100; Console.WriteLine(score[1]);输出100七、数组长度 LengthLength 表示数组元素数量。int[] nums { 10, 20, 30, 40 }; Console.WriteLine(nums.Length);输出4八、遍历数组方法一for最常用int[] nums { 10, 20, 30, 40 }; for (int i 0; i nums.Length; i) { Console.WriteLine(nums[i]); }输出10 20 30 40为什么使用Length如果以后数组变成int[] nums {1,2,3,4,5,6,7};循环无需修改。方法二foreachint[] nums { 10, 20, 30, 40 }; foreach (int item in nums) { Console.WriteLine(item); }输出10 20 30 40foreach 特点优点简洁不容易越界推荐用于只读遍历缺点不能修改元素。错误示例foreach (int item in nums) { item 100; //错误 }九、数组越界例如int[] nums { 1, 2, 3 }; Console.WriteLine(nums[3]);数组只有0 1 2访问3运行结果IndexOutOfRangeException即索引超出数组范围。十、数组默认值不同类型默认值不同。类型默认值int0double0boolfalsestringnullchar\0objectnull例如string[] names new string[3]; Console.WriteLine(names[0] null);输出True十一、数组排序使用Array.Sort()int[] nums { 8, 2, 9, 1, 5 }; Array.Sort(nums); foreach (int i in nums) { Console.Write(i ); }输出1 2 5 8 9十二、数组反转int[] nums {1,2,3,4,5}; Array.Reverse(nums);输出5 4 3 2 1十三、查找元素使用Array.IndexOf()例如int[] nums {10,20,30,40}; int index Array.IndexOf(nums,30); Console.WriteLine(index);输出2如果找不到Array.IndexOf(nums,100);输出-1十四、复制数组int[] nums {1,2,3}; int[] copy new int[3]; Array.Copy(nums, copy, 3);结果copy 1 2 3十五、多维数组二维数组int[,] matrix { {1,2,3}, {4,5,6} };内存1 2 3 4 5 6访问Console.WriteLine(matrix[1,2]);输出6遍历for (int i 0; i matrix.GetLength(0); i) { for (int j 0; j matrix.GetLength(1); j) { Console.Write(matrix[i, j] ); } Console.WriteLine(); }输出1 2 3 4 5 6十六、交错数组Jagged Array交错数组是数组中的每个元素仍然是一个数组每一行长度可以不同。int[][] jagged { new int[]{1,2}, new int[]{3,4,5}, new int[]{6} };访问Console.WriteLine(jagged[1][2]);输出5二维数组与交错数组区别二维数组1 2 3 4 5 6 7 8 9交错数组1 2 3 4 5 6十七、数组作为参数static void Print(int[] nums) { foreach (int n in nums) { Console.WriteLine(n); } }调用int[] arr {1,2,3}; Print(arr);十八、数组作为返回值static int[] GetNumbers() { return new int[] { 10, 20, 30 }; }调用int[] nums GetNumbers();十九、params 参数可变参数static int Sum(params int[] nums) { int total 0; foreach (int i in nums) total i; return total; }调用Console.WriteLine(Sum(1,2,3)); Console.WriteLine(Sum(1,2,3,4,5));输出6 15二十、数组与 ListT 的区别特性数组ArrayListT长度固定可动态扩容增删元素不方便非常方便性能略高略低扩容时有开销适用场景元素数量固定元素数量不确定示例数组int[] nums {1,2,3};ListListint nums new Listint(); nums.Add(1); nums.Add(2); nums.Add(3);二十一、综合练习示例计算平均成绩using System; class Program { static void Main() { int[] scores { 88, 92, 75, 99, 81 }; int sum 0; for (int i 0; i scores.Length; i) { sum scores[i]; } double average (double)sum / scores.Length; Console.WriteLine($总分{sum}); Console.WriteLine($平均分{average:F2}); } }输出总分435 平均分87.00二十二、常见错误总结错误原因正确做法数组越界索引超出范围使用0 ~ Length - 1未初始化数组数组为null使用new创建数组类型不一致数组元素类型必须一致使用统一的数据类型修改foreach变量foreach变量是只读的使用for修改元素二十三、学习总结数组Array是存储同一类型数据的固定长度集合。使用索引0 开始快速访问元素时间复杂度通常为O(1)。使用Length获取元素数量遍历时推荐结合for或foreach。常用静态方法包括Array.Sort()、Array.Reverse()、Array.IndexOf()、Array.Copy()。C# 支持一维数组、二维数组、多维数组和交错数组。当数据数量固定时数组是高效的选择当需要频繁增删或动态扩容时更推荐使用ListT。