C#多维数组用法(原创)--王超C#
int[,] a={{1,2,3},{4,5,6}}; //错误写法a={{1,2,3},{4,5,6}}; Console.WriteLine(a[1,1]); Console.ReadKey();
我们声明一个二维数组,有三种赋值方式:
int[,] a = { {1,2,3},{4,5,6}}; int[,] b = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; int[,] c = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
我们访问多维数组,可以使用for或foreach:
int[,] a = { {1,2,3},{4,5,6}}; int[,] b = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; int[,] c = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }; foreach (int i in a) { Console.WriteLine(i); } for (int i = 0; i < 2; i++) for (int j = 0; j < 3; j++) { Console.WriteLine(b[i,j]); } Console.ReadKey();
浙公网安备 33010602011771号