数组
数组是一个存储相同类型元素的固定大小的顺序集合。
static void Main(string[] args) { //数组A int[,] a = new int[3, 3] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, }; //数组B int[,] b = new int[3, 3] { { 4, 5, 6 }, { 7, 8, 9 }, { 1, 2, 3 }, }; //输出数组A Console.WriteLine("输出数组A"); for (int i = 0; i < a.GetLength(0); i++) { for (int j = 0; j < a.GetLength(1); j++) { Console.Write(a[i, j]+" "); } Console.WriteLine(); Console.WriteLine(); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); //输出数组B Console.WriteLine("输出数组B"); for (int i = 0; i < b.GetLength(0); i++) { for (int j = 0; j < b.GetLength(1); j++) { Console.Write(a[i, j]+" "); } Console.WriteLine(); Console.WriteLine(); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); //数组C Console.WriteLine("输出数组C"); int[,] c = new int[3, 3]; for (int i = 0; i < c.GetLength(0); i++) { for (int j = 0; j < c.GetLength(1); j++) { Console.Write(a[i, j] + b[i, j]+" "); } Console.WriteLine(); Console.WriteLine(); } Console.ReadKey(); }
数组A+数组B=数组C
然后Foreach循环遍历控制台输出结果
C# 数组(Array)
数组是一个存储相同类型元素的固定大小的顺序集合