从C#中的HashSet中删除所有元素
要从HashSet中删除所有元素,代码如下-
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args){
HashSet<string> set1 = new HashSet<string>();
set1.Add("A");
set1.Add("B");
set1.Add("C");
set1.Add("D");
set1.Add("E");
set1.Add("F");
set1.Add("G");
set1.Add("H");
Console.WriteLine("Elements in HashSet1...");
foreach (string res in set1){
Console.WriteLine(res);
}
HashSet<string> set2 = new HashSet<string>();
set2.Add("John");
set2.Add("Jacob");
set2.Add("Ryan");
set2.Add("Tom");
set2.Add("Andy");
set2.Add("Tim");
set2.Add("Steve");
set2.Add("Mark");
Console.WriteLine("Elements in HashSet2...");
foreach (string res in set2){
Console.WriteLine(res);
}
Console.WriteLine("Is HashSet1 equal to HashSet2? = "+set1.Equals(set2));
Console.WriteLine("Count of HashSet2 = "+set2.Count);
set2.Clear();
Console.WriteLine("Count of HashSet2 (updated) = "+set2.Count);
}
}输出结果
这将产生以下输出-
Elements in HashSet1... A B C D E F G H Elements in HashSet2... John Jacob Ryan Tom Andy Tim Steve Mark Is HashSet1 equal to HashSet2? = False Count of HashSet2 = 8 Count of HashSet2 (updated) = 0
示例
让我们看另一个例子-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args){
HashSet<string> set1 = new HashSet<string>();
set1.Add("A");
set1.Add("B");
set1.Add("C");
set1.Add("D");
set1.Add("E");
set1.Add("F");
set1.Add("G");
set1.Add("H");
Console.WriteLine("Elements in HashSet...");
foreach (string res in set1){
Console.WriteLine(res);
}
Console.WriteLine("Count of HashSet1 = "+set1.Count);
set1.Clear();
Console.WriteLine("Count of HashSet1 (updated) = "+set1.Count);
}
}输出结果
这将产生以下输出-
Elements in HashSet... A B C D E F G H Count of HashSet1 = 8 Count of HashSet1 (updated) = 0
热门推荐
7 祝福语简短的微信
10 退休的祝福语简短精辟
11 最新高考祝福语 简短
12 祝福语短句简短好听霸气
13 年底了祝福语简短的
14 保洁阿姨送来祝福语简短
15 孩子语音婚礼祝福语简短
16 给儿子新婚祝福语简短
17 高考成绩高分祝福语简短
18 生日奶茶红包祝福语简短