เพื่อเริ่มต้น HashSet
var h = new HashSet<string>(arr1);
ด้านบน เราได้ตั้งค่าอาร์เรย์ใน HashSet ต่อไปนี้เป็นอาร์เรย์ −
string[] arr1 = {
"electronics",
"accessories”,
"electronics",
}; ต่อไปนี้เป็นตัวอย่างที่แสดงวิธีการใช้ HashSet ใน C# -
ตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
string[] arr1 = {
"electronics",
"accessories”,
"electronics",
};
Console.WriteLine(string.Join(",", arr1));
// HashSet
var h = new HashSet(arr1);
// eliminates duplicate words
string[] arr2 = h.ToArray();
Console.WriteLine(string.Join(",", arr2));
}
}