舉例如下
class Demo {
static void Main() {
int[] a = new int[5];
int i = 0;
while (i < 5) {
a[i] = ++i;
System.Console.WriteLine(i);
}
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:array1.cs
功能:示範 C# 程式
作者:張凱慶
時間:西元 2013 年 6 月 */編譯執行,結果如下

陣列屬於物件,因此建立陣列要用到關鍵字 (keyword) new
int[] a = new int[5];
下例示範用大括弧直接建立元素初值
class Demo {
static void Main() {
string[] a = new string[4] {"Tony", "Judy", "John", "Mary"};
foreach (string i in a) {
System.Console.WriteLine(i);
}
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:array2.cs
功能:示範 C# 程式
作者:張凱慶
時間:西元 2013 年 6 月 */編譯執行,結果如下

陣列的元素也可以是陣列,這樣的陣列是多維的,例如
class Demo {
static void Main() {
int[,] a = new int[,] {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
foreach (int i in a) {
System.Console.WriteLine(i);
}
int b = a[3, 0];
System.Console.WriteLine(b);
b = a[2, 1];
System.Console.WriteLine(b);
b = a[2, 0];
System.Console.WriteLine(b);
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:array3.cs
功能:示範 C# 程式
作者:張凱慶
時間:西元 2013 年 6 月 */編譯執行,結果如下

| 中英文術語對照 | |
|---|---|
| 陣列 | array |
| 資料結構 | data structure |
| 資料型態 | data type |
| 關鍵字 | keyword |
您可以繼續參考
其他內建型態
相關目錄
回 C# 快速導覽
回 C# 教材
回首頁
參考資料
Standard ECMA-334 C# Language Specification
msdn: 陣列 (C# 程式設計手冊)
沒有留言:
張貼留言