Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

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 = new HashSet(arr1);

      // eliminates duplicate words
      string[] arr2 = h.ToArray();

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

HashSet을 선언합니다.

var h = new HashSet<string7gt;(arr1);

이제 중복 단어를 제거하기 위해 배열에 설정하십시오 -

string[] arr2 = h.ToArray();