Để khởi tạo HashSet.
var h = new HashSet<string>(arr1);
Ở trên, chúng ta đã thiết lập một mảng trong HashSet. Sau đây là mảng -
string[] arr1 = {
"electronics",
"accessories”,
"electronics",
}; Sau đây là một ví dụ cho thấy cách triển khai HashSet trong C # -
Ví dụ
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));
}
}