Home  >  Article  >  Backend Development  >  Initializing a HashSet in C#

Initializing a HashSet in C#

WBOY
WBOYforward
2023-08-29 14:57:061038browse

在 C# 中初始化 HashSet

Initialize a HashSet.

var h = new HashSet<string>(arr1);

Above, we set an array in HashSet. The following is an array -

string[] arr1 = {
   "electronics",
   "accessories&rdquo;,
   "electronics",
};

The following example shows how to implement a HashSet in C# -

Example

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

class Program {
   static void Main() {
      string[] arr1 = {
         "electronics",
         "accessories&rdquo;,
         "electronics",
      };

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

      // HashSet
      var h = new HashSet(arr1);
      // eliminates duplicate words
      string[] arr2 = h.ToArray();
      Console.WriteLine(string.Join(",", arr2));
   }
}

The above is the detailed content of Initializing a HashSet in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Tuple class in C#Next article:Tuple class in C#