从C#中的集合中删除所有元素
要从集合中删除所有元素,代码如下-
示例
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection<int> col = new Collection<int>();
col.Add(10);
col.Add(20);
col.Add(30);
col.Add(40);
col.Add(50);
col.Add(60);
col.Add(70);
col.Add(80);
Console.WriteLine("Elements in the Collection...");
foreach(int val in col) {
Console.WriteLine(val);
}
Console.WriteLine("Does the collection has the element 70? = "+col.Contains(70));
Console.WriteLine("Count of elements = " + col.Count);
col.Clear();
Console.WriteLine("Count of elements (updated) = " + col.Count);
}
}输出结果
这将产生以下输出-
Elements in the Collection... 10 20 30 40 50 60 70 80 Does the collection has the element 70? = True Count of elements = 8 Count of elements (updated) = 0
示例
让我们看另一个例子-
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection<String> col = new Collection<String>();
col.Add("A");
col.Add("B");
col.Add("C");
col.Add("D");
col.Add("E");
Console.WriteLine("Elements in the Collection...");
foreach(String val in col) {
Console.WriteLine(val);
}
Console.WriteLine("Does the collection has the element D? = "+(col.Contains("D")));
Console.WriteLine("Count of elements = " + col.Count);
col.Clear();
Console.WriteLine("Count of elements (updated) = " + col.Count);
}
}输出结果
这将产生以下输出-
Elements in the Collection... A B C D E Does the collection has the element D? = True Count of elements = 5 Count of elements (updated) = 0
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短