什麼是 C# 中的泛型列表?


Generic List<T>是 C# 中的泛型集合。與陣列不同,它可以使用 List 動態增加大小。

我們來看一個示例 -

我們首先設定列表 -

List<string> myList = new List<string>()

現在向列表中新增元素 -

List<string> myList = new List<string>() {
   "mammals",
   "reptiles",
   "amphibians"
}

現在使用屬性來計算已新增元素的數量 -

示例

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      List<string> myList = new List() {
         "mammals",
         "reptiles",
         "amphibians"
      };
      Console.WriteLine(myList.Count);
   }
}

更新於:20-Jun-2020

2K+ 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

立即開始
廣告
© . All rights reserved.