什么是 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.