从C#中的另一个集合创建HashSet
要从另一个Collection创建HashSet,代码如下-
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
HashSet<int> set1 = new HashSet<int>();
set1.Add(100);
set1.Add(200);
set1.Add(300);
Console.WriteLine("HashSet created from another collection...");
foreach(int i in set1){
Console.WriteLine(i);
}
HashSet<int> set2 = new HashSet<int>(set1);
Console.WriteLine("HashSet created from another collection...");
foreach(int i in set2){
Console.WriteLine(i);
}
}
}输出结果
这将产生以下输出-
HashSet created from another collection... 100 200 300 HashSet created from another collection... 100 200 300
示例
现在让我们来看另一个示例-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
HashSet<string> set1 = new HashSet<string>();
set1.Add("Jacob");
set1.Add("Tom");
set1.Add("Harry");
set1.Add("Harry");
set1.Add("Tom");
set1.Add("Harry");
Console.WriteLine("HashSet created from another collection...");
foreach(string i in set1){
Console.WriteLine(i);
}
HashSet<string> set2 = new HashSet<string>(set1);
Console.WriteLine("HashSet created from another collection...");
foreach(string i in set2){
Console.WriteLine(i);
}
}
}输出结果
这将产生以下输出-
HashSet created from another collection... Jacob Tom Harry HashSet created from another collection... Jacob Tom Harry
热门推荐
10 国庆中秋祝福语简短搞笑
11 相恋人回去祝福语简短
12 结婚对白誓言简短祝福语
13 牛年除夕祝福语简短的
14 给干爸过年祝福语简短
15 美好的成语祝福语简短
16 师生聚餐的祝福语简短
17 国庆求婚祝福语简短精辟
18 外婆祝福语短句唯美简短