Home > Backend Development > C#.Net Tutorial > Initializing a HashSet in C#

Initializing a HashSet in C#

WBOY
Release: 2023-08-29 14:57:06
forward
1118 people have browsed it

在 C# 中初始化 HashSet

Initialize a HashSet.

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

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

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

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));
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template