C# HashSet HashSet(IEqualityComparer)

Description

HashSet HashSet (IEqualityComparer ) initializes a new instance of the HashSet class that is empty and uses the specified equality comparer for the set type.

Syntax


public HashSet(
  IEqualityComparer<T> comparer
)

Parameters

  • comparer - The IEqualityComparer implementation to use when comparing values in the set, or null to use the default EqualityComparer implementation for the set type.

Example

The code below creates a new instance of the HashSet<T> class that uses the specified equality comparer.


/*from   www.j  a v a2  s  .  c  o  m*/
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        MyComparer myComparer = new MyComparer();
        HashSet<string> allVehicles = new HashSet<string>(myComparer);

        allVehicles.Add("A");
        allVehicles.Add("T");
        allVehicles.Add("B");
    }
}

class MyComparer : EqualityComparer<string>
{
    public override bool Equals(string s1, string s2)
    {
        return s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase);
    }
    public override int GetHashCode(string s)
    {
        return base.GetHashCode();
    }
}




















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack