C# HashSet HashSet(IEqualityComparer)
Description
HashSet
initializes
a new instance of the HashSet
Syntax
public HashSet(
IEqualityComparer<T> comparer
)
Parameters
comparer
- The IEqualityComparerimplementation 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();
}
}