List <T>是C#中的集合,并且是通用集合。C#列表中使用add和remove方法添加和删除元素。
让我们看看如何Add()在C#中使用方法。
using System;
using System.Collections.Generic;
class Program {
   static void Main() {
      List<string> sports = new List<string>();
      sports.Add("Football");
      sports.Add("Tennis");
      sports.Add("Soccer");
      foreach (string s in sports) {
         Console.WriteLine(s);
      }
   }
}输出结果
Football Tennis Soccer
让我们看看如何Remove()在C#中使用方法。
using System;
using System.Collections.Generic;
class Program {
   static void Main() {
      List<string> sports = new List<string>();
      sports.Add("Football"); // add method
      sports.Add("Tennis");
      sports.Add("Soccer");
      Console.WriteLine("Old List...");
      foreach (string s in sports) {
         Console.WriteLine(s);
      }
      Console.WriteLine("New List...");
      sports.Remove("Tennis"); // remove method
      foreach (string s in sports) {
         Console.WriteLine(s);
      }
   }
}输出结果
Old List... Football Tennis Soccer New List... Football Soccer