Questions tagged [iequalitycomparer]

IEqualityComparer is a .NET framework interface that allows the implementation of customized equality comparison for collections. That is, you can create your own definition of equality, and specify that this definition be used with a collection type that accepts the IEqualityComparer interface. Supported in .NET versions 3.5, 3.0, 2.0 (Source: MSDN)

This interface allows the implementation of customized equality comparison for collections. That is, you can create your own definition of equality, and specify that this definition be used with a collection type that accepts the IEqualityComparer interface. In the .NET Framework, constructors of the Hashtable, NameValueCollection, and OrderedDictionary collection types accept this interface.

This interface supports only equality comparisons. Customization of comparisons for sorting and ordering is provided by the IComparer interface.

For the generic version of this interface, see System.Collections.Generic.IEqualityComparer(Of T).

The following code example demonstrates the implementation of a case-insensitive IEqualityComparer. In this example, the CaseInsensitiveComparer.Compare method is used to determine whether two objects are equal, based on the provided CultureInfo.

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    publicnewbool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            returntrue;
        }
        else
        {
            returnfalse;
        }
    }

    publicint GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

Source: MSDN

282 questions
3
votes
1 answer

Breakpoint in GetHashCode of IEqualityComparer + GroupBy not hit

Code example: var positions = new List(); for (int i = 0; i < 20; i++) { positions.Add(new Position { Code = "A", Value = i }); positions.Add(new Position { Code = "A", Value = i }); } var test = positions.GroupBy(p => p,…
Gerard
  • 13,023
  • 14
  • 72
  • 125
3
votes
3 answers

Make List Distinctable with IEquatable

I wanted to make my Class Sortable(By Age) when it stored in List. I read this : IComparable Vs IComparer and I maked my class Sortable . public class Student : IComparable { public int ID { get; set; } public string Name { get;…
Parsa
  • 7,995
  • 2
  • 27
  • 37
3
votes
1 answer

How to make IEqualityComparer for generic types

I want an IEqualityComparer that returns true if and only if two generic types are the same ignoring generic parameters. So comparer.Equals(typeof(List), typeof(List)) should return true. I am doing a comparison by Name: public class…
Little Endian
  • 784
  • 8
  • 19
3
votes
2 answers

Using IEqualityComparer GetHashCode with a tolerance

I am trying to implement an IEqualityComparer that has a tolerance on a date comparison. I have also looked into this question. The problem is that I can't use a workaround because I am using the IEqualityComparer in a LINQ .GroupJoin(). I have…
Matt Rowland
  • 4,575
  • 4
  • 25
  • 34
3
votes
2 answers

Utilizing the GetHashCode() part of IEqualityComparer for direct comparisons?

I've written a class deriving from IEqualityComparer which works great for the LINQ query I needed it for. As I understand it, GetHashCode() (fast) is called first, then Equals()(slightly slower) if the hashcode is the same, for such…
maxp
  • 24,209
  • 39
  • 123
  • 201
3
votes
1 answer

IEqualityComparer for nullable struct

I want to write an equality comparer for Nullable structs. Lets say, DateTime?. So I come up with this code: public class NullableEntityComparer : IEqualityComparer where TType : struct where TEntity :…
3
votes
2 answers

How to return a specific item in Distinct using EqualityComparer in C#

I have defined a CustomListComparer which compares List A and List B and if Union of the two lists equals at least on of the lists, considers them equal. var distinctLists = MyLists.Distinct(new CustomListComparer()).ToList(); public…
Vahid
  • 5,144
  • 13
  • 70
  • 146
3
votes
3 answers

Comparing two List in C#

I have a class called MyClass This class inherits IEquatable and implements equals the way I need it to. (Meaning: when I compare two MyClass tyupe objects individually in code, it works) I then create two List: var ListA = new…
Matt
  • 25,943
  • 66
  • 198
  • 303
3
votes
1 answer

How to make an EqualityComparer compare against two fields?

Our code base currently has the following EqualityComparer. public static IEnumerable Exclude(this IEnumerable first, IEnumerable second, …
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
3
votes
2 answers

EqualityComparerer.Default.Equals() vs object.Equals() and polymorphism

Once again discussing equality I stumbled on EqualityComparer.Default.Equals(). I prefer to call this method for reference types rather than object.Equals(). Now I think I was dreadfully wrong. object.Equals() uses overridable instance Equals()…
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
3
votes
2 answers

Can this implementation of an IEqualityComparer be improved?

I don't see any problems with this code, but it feels like I'm missing something. Maybe it is possible to reduce the number of lines. Or is there even a bug to be fixed? I'm open to any suggestions. public class NameComparer :…
mafu
  • 31,798
  • 42
  • 154
  • 247
3
votes
1 answer

Why doesn't IEqualityComparer extend IEqualityComparer in .NET

In .NET, the fact that IEnumerable extends IEnumerable often comes in handy. Frustratingly, though IEqualityComparer and IComparer do not extend their non-generic counterparts, despite the fact that the EqualityComparer and Comparer
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
3
votes
2 answers

Using IEqualityComparer to find records

I'm using the following IEqualityComparer to strip special characters from a company name before comparison as follows: public class CompanyNameIgnoringSpaces : IEqualityComparer { public bool Equals(LeadGridViewModel…
Nick
  • 5,844
  • 11
  • 52
  • 98
2
votes
1 answer

How do you get WCF serialization to preserve a non-default Comparer on a generic Dictionary?

Suppose we start from scratch in Visual Studio 2010 and add a 'WCF Service Aplication'. We add this method and implementation: // (in IService1.cs) [OperationContract] Dictionary GetDictionary(); // (in Service1.svc.cs) …
AakashM
  • 62,551
  • 17
  • 151
  • 186
2
votes
2 answers

IEqualityComparer.Equals when used with IEnumerable.Contains is x or y the value in the list?

IEnumberable has an extension method Contains which takes two parameters. The first parameter is the value to check for and the second is an implementation of IEqualityComparer. Looking at IEqualityComparer.Equals it takes two parameters named x…
Robert MacLean
  • 38,975
  • 25
  • 98
  • 152