在C#中初始化HashSet
初始化HashSet。
var h = newHashSet-<string>(arr1);
上面,我们在HashSet中设置了一个数组。以下是数组-
string[] arr1 = { "electronics", "accessories”, "electronics", };
以下是显示如何在C#中实现HashSet的示例-
示例
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 = newHashSet-(arr1); //消除重复的单词 string[] arr2 = h.ToArray(); Console.WriteLine(string.Join(",", arr2)); } }