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
7
votes
1 answer

I need to strip all the symbols from a string in order to create an `IEqualityComparer` that ignores punctuation symbols

In part of my application I have an option that displays a list of albums by the current artist that aren't in the music library. To get this I call a music API to get the list of all albums by that artist and then I remove the albums that are in…
ChrisF
  • 134,786
  • 31
  • 255
  • 325
7
votes
5 answers

Doing Distinct() using base class IEqualityComparer, and still return the child class type?

I have a number of classes that derive from a class BaseClass where BaseClass just has an `Id property. I now need to do distinct on a collections of some of these objects. I have the following code over and over for each of the child…
leora
  • 188,729
  • 360
  • 878
  • 1,366
7
votes
1 answer

GetHashCode() for long primitive

I am writing a EqualityComparer for a LINQ distinct expression and I am not too sure about the GetHashCode overload method. Would the below code be correct? The Id property is a long primitive. public int GetHashCode(Deal obj) { return…
yulun
  • 260
  • 1
  • 6
  • 21
7
votes
6 answers

Is it possible to write a hash code function for an comparer that matches many-to-many?

Can I write a hash code function for the following comparer logic? Two instances of My are equal if at least two properites from (A, B, C) match. The Equals part is simple, but I'm stumped on the hash code part, and part of me is thinking it might…
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
6
votes
3 answers

EqualityComparer.Default misunderstanding?

I have a class Person, it implements Equals() method from IEquatable (also overrides Object.Equals method, lets ignore the GetHashcode() method for now) class Person : IEquatable { public string Name { get; set; } public…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
6
votes
2 answers

HashSet constructor with custom IEqualityCompare defined by lambda?

Currently the HashSet constructor that allows you to define your equality comparison yourself is the HashSet(IEqualityComparer comparer) constructor. I would like to define this EqualityComparer as a lambda. I found this blog post that…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
6
votes
2 answers

Why we need the IEqualityComparer,IEqualityComparer interface?

the 'Equal' and 'GetHashcode' method are exist in the object class, and our type inherit the object base class. what's the different between implement the two methods of the object directly and using the IComparer interface? if we overriding…
randall
  • 151
  • 2
  • 9
6
votes
2 answers

Value vs. Reference equality in generic List.Contains()

Attempt #3 to simplify this question: A generic List can contain any type - value or reference. When checking to see if a list contains an object, .Contains() uses the default EqualityComparer for type T, and calls .Equals() (is my…
James King
  • 6,233
  • 5
  • 42
  • 63
6
votes
4 answers

Is there a way to derive IEqualityComparer from IComparer?

TL;DR I'm looking for a way to obtain IEqualityComparer from IComparer, no matter which datatype is T, including case-insensitive options if T is string. Or I need a different solution for this problem. Here's full story: I'm implementing…
Endrju
  • 2,354
  • 16
  • 23
6
votes
2 answers

IEqualityComparer for Annoymous Type

Firstly I have seen IEqualityComparer for anonymous type and the answers there do not answer my question, for the obvious reason that I need an IEqualityComparer not and IComparer for use with Linq's Distinct() method. I have checked the other…
MoonKnight
  • 23,214
  • 40
  • 145
  • 277
6
votes
1 answer

Custom equality comparer for Type in dictionary

Since Int32 is a Object, I want this to print "True" Dictionary dict = new Dictionary(new MyComparer()); dict[typeof(object)] = "Hello"; Console.WriteLine(dict.ContainsKey(typeof(int))); // currently prints…
vexe
  • 5,433
  • 12
  • 52
  • 81
6
votes
1 answer

Remove duplicates from DataTable and custom IEqualityComparer

How have I to implement IEqualityComparer to remove duplicates rows from a DataTable with next structure: ID primary key, col_1, col_2, col_3, col_4 The default comparer doesn't work because each row has it's own, unique primary key. How…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
6
votes
4 answers

Custom Generic.IEqualityComparer(Of T) - Compiler Errors

I am trying to implement a simple IEqulityComparer to use with LINQ collections. I have written the following code which is reduced to its simplest form for discussion purposes... Public Structure bob Dim SiteID As Integer Dim fred As…
FourOaks
  • 190
  • 1
  • 3
  • 7
5
votes
3 answers

Questions about IEqualityComparer / List.Distinct()

Here is the equality comparer I just wrote because I wanted a distinct set of items from a list containing entities. class InvoiceComparer : IEqualityComparer { public bool Equals(Invoice x, Invoice y) { …
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
5
votes
1 answer

Custom object using Except failing to use IEqualityComparer

here is the object code: public class DlpItem : IEqualityComparer { public string Text { get; set; } public int Id { get; set; } public DlpItem(int pId) { Text = string.Empty; Id = pId; } public…
Matthew Cox
  • 13,566
  • 9
  • 54
  • 72
1 2
3
18 19