Home [C# 筆記] 陣列常用的屬性(Attribute)
Post
Cancel

[C# 筆記] 陣列常用的屬性(Attribute)

「陣列」所代表就是一個「物件」,既然陣列是一個物件,那麼也表示它應該還提供了一些操作陣列的屬性和方法。

常用的屬性

  • Length:陣列裡所有維度的元素總和(不規則陣列僅顯示第一維度的長度)。
  • Count:和Length功能相同。
  • Rank:顯示陣列的維度。

Length 長度

Length用來取得陣列的長度(陣列的元素總數)。

1
2
3
4
5
6
7
8
9
10
11
12
static void Main(string[] args)
{
    int[,] lottery = {
        { 02,33,06,02,04,11},
        { 12,23,26,35,07,01}
    };

    Console.WriteLine($"陣列長度為:{lottery.Length}");
}

//執行結果:
//陣列長度為:12

Rank 維度

Rank用來取得陣列的「維度」。

1
2
3
4
5
6
7
8
static void Main(string[] args)
{
    int[,,] arr3D = new int[2, 4, 8];
    Console.WriteLine($"這是 {arr3D.Rank} 維陣列");
}

//執行結果:
//這是 3 維陣列

[C# 筆記] array 陣列 by R
[C# 筆記] 陣列(Array)的宣告 by R
Book: Visual C# 2005 建構資訊系統實戰經典教本

This post is licensed under CC BY 4.0 by the author.