什么是C#中的HashSet,C#Set集合?

C#中的HashSet消除了数组中重复的字符串或元素。在C#中,这是一个优化的集合集合。

让我们看一个使用C#HashSet删除重复字符串的示例-

示例

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      string[] arr1 = {
         "one",
         "two",
         "two",
         "one",
         "three"
      };

      Console.WriteLine(string.Join(",", arr1));

      //HashSet-
      var h = newHashSet-(arr1);

      //消除重复的单词
      string[] arr2 = h.ToArray();

      Console.WriteLine(string.Join(",", arr2));
   }
}

声明HashSet。

var h = newHashSet-<string7gt;(arr1);

现在在数组上设置它以删除重复的单词-

string[] arr2 = h.ToArray();