다른 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