在C#中,数组声明有以下几种技巧:
new
来声明数组并初始化:int[] numbers = new int[5];
{}
来初始化数组元素:int[] numbers = {1, 2, 3, 4, 5};
var
关键字来简化声明:var numbers = new int[5];
Array
类的静态方法创建数组:int[] numbers = Array.CreateInstance(typeof(int), 5) as int[];
int[,] matrix = new int[3, 3];
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[3];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];