# 陣列 (C# 程式設計手冊)Arrays (C# Programming Guide)

type[] arrayName;

class TestArraysClass
{
static void Main()
{
// Declare a single-dimensional array.
int[] array1 = new int[5];

// Declare and set array element values.
int[] array2 = new int[] { 1, 3, 5, 7, 9 };

// Alternative syntax.
int[] array3 = { 1, 2, 3, 4, 5, 6 };

// Declare a two dimensional array.
int[,] multiDimensionalArray1 = new int[2, 3];

// Declare and set array element values.
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

// Declare a jagged array.
int[][] jaggedArray = new int[6][];

// Set the values of the first array in the jagged array structure.
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
}
}


## 陣列概觀Array Overview

• 陣列可以是一維多維不規則An array can be Single-Dimensional, Multidimensional or Jagged.

• 當陣列執行個體建立時，也會建立維度的數目和每個維度的長度。The number of dimensions and the length of each dimension are established when the array instance is created. 在執行個體的存留期期間，這些值無法變更。These values can't be changed during the lifetime of the instance.

• 數值陣列元素的預設值設定為零，且參考元素會設定為 null。The default values of numeric array elements are set to zero, and reference elements are set to null.

• 不規則陣列為陣列的陣列，因此其元素為參考類型，且會初始化為 nullA jagged array is an array of arrays, and therefore its elements are reference types and are initialized to null.

• 陣列為以零為基底索引：具有 n 個元素的陣列，會從 0n-1 編製索引。Arrays are zero indexed: an array with n elements is indexed from 0 to n-1.

• 陣列元素可以是任何類型，包含陣列類型。Array elements can be of any type, including an array type.

• 陣列類型是衍生自抽象基底類型 Array參考類型Array types are reference types derived from the abstract base type Array. 由於這個類型會實作 IEnumerableIEnumerable<T>，您可以在所有以 C# 撰寫的陣列上使用 foreach 反覆項目。Since this type implements IEnumerable and IEnumerable<T>, you can use foreach iteration on all arrays in C#.