什么是C#列表中的添加,删除方法?
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
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短