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
5
votes
3 answers

Best way to compare two Dictionary for equality

Is this the best way to create a comparer for the equality of two dictionaries? This needs to be exact. Note that Entity.Columns is a dictionary of KeyValuePair(string, object) : public class EntityColumnCompare : IEqualityComparer { …
Sean Thoman
  • 7,429
  • 6
  • 56
  • 103
5
votes
3 answers

EqualityComparer.Default doesn't return the derived EqualityComparer

I have a class Person, and created an equality comperer class derived from EqualityComparer < Person >. Yet the default EqualityComparer doesn't call the Equals function of my equality comparer According to MSDN EqualityComparer < T > .Default…
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
5
votes
1 answer

Collection priority in LINQ Intersect, Union, using IEqualityComparer

If I have two collections of type T, and an IEqualityComparer that compares a subset of their properties, which collection would the resulting elements of an Intersect or Union come from? The tests I've run so far suggest the following: the item(s)…
trilson86
  • 939
  • 1
  • 9
  • 20
5
votes
2 answers

How to use Object.GetHashCode() on a type that overrides GetHashCode()

I have a class A that implements IEquatable<>, using its fields (say, A.b and A.c) for implementing/overriding Equals() and overriding GetHashCode(), and everything works fine, 99% of the time. Class A is part of a hierarchy (class B, C) that all…
Jimmy
  • 5,131
  • 9
  • 55
  • 81
5
votes
3 answers

Using an IEqualityComparer with a LINQ to Entities Except clause

I have an entity that I'd like to compare with a subset and determine to select all except the subset. So, my query looks like this: Products.Except(ProductsToRemove(), new ProductComparer()) The ProductsToRemove() method returns a List
Jason N. Gaylord
  • 7,910
  • 15
  • 56
  • 95
5
votes
4 answers

C# - List.Remove() always deletes the first object on the list

Working in Visual Studio 2008 (C#)... I use a List collection to store instances of my custom class (Shift). I want to delete a certain shift from the list by using the Remove method. But List.Remove() always deletes the first item it finds. I've…
Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91
5
votes
2 answers

Implementing IEqualityComparer for comparing arbitrary properties of any class (including anonymous)

I am writing this neat class implementing IEqualityComparer, so that I can just pass any anonymous type to it (or actually any type with properties) and it will automatically compare the types by comparing the property values of the types. public…
cdbeelala89
  • 2,066
  • 3
  • 28
  • 39
4
votes
2 answers

EqualityComparer.Default.Equals() returning wrong result or what?

Is there an explanation for this other than being a bug in the .NET Framework? The EqualityComparer.Default.Equals() method is saying that the following URLs are…
HappyNomad
  • 4,458
  • 4
  • 36
  • 55
4
votes
3 answers

Why we implement GetHashCode in IEqualityComparer?

I want to get distinct items from List in C# by using IEqualityComparer interface. But I don't know about GetHashCode. I have implement both GetHashCode and Equals methods. And how can I call Equals method to get distinct items from a list having…
NASSER
  • 5,900
  • 7
  • 38
  • 57
4
votes
3 answers

When should I use IEqualityComparer? C#

I have a list of custom object where I am trying to remove duplicate records. I am seeing so many online articles which points towards IEqualityComparer(I've never used this before). Question is, when should I use it? I can achieve same result by…
GThree
  • 2,708
  • 7
  • 34
  • 67
4
votes
3 answers

how to force HashSet to rehash members?

In this situation where one member is edited to become equal to another, what is the proper way to force the HashSet to recalculate hashes and thereby purge itself of duplicates? I knew better than to expect this to happen automatically, so I tried…
4
votes
2 answers

Why can I compare an object to an IEnumerable of those objects but not a List?

I've just realised I'd written code that I'd expect to be invalid (with regard to syntax), but the compiler accepts. For the sake of brevity I've recreated an example. Given a type: private class MyType { } I can then compare an instance of MyType…
user1017882
4
votes
4 answers

HashSet.CreateSetComparer() cannot specify IEqualityComparer, is there an alternative?

In the internal source there is such a constructor public HashSetEqualityComparer(IEqualityComparer comparer) but it's internal so I can't use it. By default, HashSet.CreateSetComparer() just uses the parameterless constructor which will…
David S.
  • 5,965
  • 2
  • 40
  • 77
4
votes
1 answer

When to use IEqualityComparer in an app targeting .NET 4.0

Is there any benefit for me to implement the weakly typed IEqualityComparer in .NET 4.0 apps in addition to the IEqualityComparer interface? Another angle is I can always implement IEqualityComparer to make up an equally weakly…
John K
  • 28,441
  • 31
  • 139
  • 229
4
votes
4 answers

C# / LINQ fastest way of comparing two lists and assigning value

I have made a code which basically compares two lists in C#. First list contains properties like this: ItemID TotalViews First list lacks values for TotalViews so I'm assigning them from 2nd list which has these props: ItemID HitCount // this is…
User987
  • 3,663
  • 15
  • 54
  • 115