Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

System.Collections.Generic.HashSetクラスを使ってみた

System.Collections.Generic.HashSetクラスを使ってみました。
HashSet(T) クラス (System.Collections.Generic)
値のセットを表します。


HashSet コレクション型
簡単に言えば、HashSet クラスは値のない Dictionary コレクションと考えることができます。 ...

「値のないDictionary」ということはどうやら重複は排除されるようですね。

検証

検証してみましょう。

using System.Collections.Generic;
using NUnit.Framework;

namespace HashSetSample {
    [TestFixture]
    public class HashSetTest {
        [Test]
        public void Test() {
            HashSet<int> hashSet = new HashSet<int>() {
                3, 1, 1, 2, 2
            };

            CollectionAssert.AreEquivalent(new int[] {1, 2, 3}, hashSet);
        }
    }
}

確かに重複は排除されていますね。