共用方式為


將陣列當做物件 (C# 程式設計手冊)

更新:2007 年 11 月

在 C# 中,陣列實際上是物件,而不是只像在 C 和 C++ 中可定址的連續記憶體區域而已。Array 是所有陣列型別的抽象基底型別。您可以使用 Array 的屬性和其他類別成員。例如,可以使用 Length 屬性取得陣列的長度。下列程式碼將 numbers 陣列的長度 (亦即 5) 指派給變數 lengthOfNumbers:

int[] numbers = { 1, 2, 3, 4, 5 };
int lengthOfNumbers = numbers.Length;

System.Array 類別還提供了許多其他有用的方法和屬性,例如,排序、搜尋和複製陣列。

範例

本範例使用 Rank 屬性顯示陣列的維度數目。

class TestArraysClass
{
    static void Main()
    {
        // Declare and initialize an array:
        int[,] theArray = new int[5, 10];
        System.Console.WriteLine("The array has {0} dimensions.", theArray.Rank);
    }
}
// Output: The array has 2 dimensions.

請參閱

概念

C# 程式設計手冊

參考

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

一維陣列 (C# 程式設計手冊)

多維陣列 (C# 程式設計手冊)

不規則陣列 (C# 程式設計手冊)