在C#中,數組聲明有以下幾種技巧:
new
來聲明數組并初始化:int[] numbers = new int[5];
{}
來初始化數組元素:int[] numbers = {1, 2, 3, 4, 5};
var
關鍵字來簡化聲明:var numbers = new int[5];
Array
類的靜態(tài)方法創(chuàng)建數組: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];