उदाहरण
HashSet<T> का एक कंस्ट्रक्टर होता है जो IEnumerable<T> स्वीकार करता है, जिससे किसी मौजूदा कलेक्शन से 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...");
foreach(int i in set1) {
Console.WriteLine(i);
}
// set1 से नया HashSet बनाएं
HashSet<int> set2 = new HashSet<int>(set1);
Console.WriteLine("अन्य कलेक्शन से बनाया गया HashSet...");
foreach(int i in set2) {
Console.WriteLine(i);
}
}
}
आउटपुट
अन्य कलेक्शन से बनाया गया HashSet... 100 200 300 अन्य कलेक्शन से बनाया गया HashSet... 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...");
foreach(string i in set1) {
Console.WriteLine(i);
}
HashSet<string> set2 = new HashSet<string>(set1);
Console.WriteLine("अन्य कलेक्शन से बनाया गया HashSet...");
foreach(string i in set2) {
Console.WriteLine(i);
}
}
}
आउटपुट
अन्य कलेक्शन से बनाया गया HashSet... Jacob Tom Harry अन्य कलेक्शन से बनाया गया HashSet... Jacob Tom Harry