C#多维数组

``````int[, ] arr=new int[3, 3];//declaration of 2D array
int[, , ] arr=new int[3, 3, 3];//declaration of 3D array``````

C＃多维数组示例

``````using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[, ] arr=new int[3, 3];//declaration of 2D array
arr[0, 1]=10;//initialization
arr[1, 2]=20;
arr[2, 0]=30;

//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i, j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}``````

``````0 10 0
0 0 20
30 0 0``````

C＃多维数组示例：同时进行声明和初始化

``int[, ] arr = new int[3, 3]= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };``

``int[, ] arr = new int[, ]{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };``

``int[, ] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };``

``````using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[, ] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//declaration and initialization

//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i, j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}``````

``````1 2 3
4 5 6
7 8 9``````