天天看點

C# 數組 使用速查

1、一維數組(值類型)

  1. int[] myIntegers; //申明一個數組的引用 
  2. myIntegers = new int[100]; //建立一個含有100個int元素的資料

2、一維數組(引用類型)

  1. Control[] myControls; //聲明一個數組引用
  2. myControls = new Control[3]; //建立一個含有3個Control引用的數組
  3. //注意:以上隻建立了3個引用,沒有建立實際的對象。
  4. myControls[0] = new Button(); //建立對象
  5. myControls[1] = new TextBox();
  6. myControls[2] = new ListView();

3、多元數組——矩陣數組

  1. double[,] myDoubles = new double[10,20]; //二維數組
  2. string[,,] myStrings = new string[3,4,5]; //三維數組

4、多元數組——鋸齒數組(jagged arrays)

  1. Point[][] myPonits = new Point[3][];
  2. myPoints[0] = new Point[10]; //注意這裡同樣隻是配置設定了10個引用,沒有建立對象
  3. myPoints[1] = new Point[5];
  4. myPoints[2] = new Point[20]; 

參考資料:《CLR via C#》