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

What is the difference between IEqualityComparer and IEquatable?

I want to understand the scenarios where IEqualityComparer and IEquatable should be used. The MSDN documentation for both looks very similar.
Tilak
  • 30,108
  • 19
  • 83
  • 131
163
votes
3 answers

What's the role of GetHashCode in the IEqualityComparer in .NET?

I'm trying to understand the role of the GetHashCode method of the interface IEqualityComparer. The following example is taken from MSDN: using System; using System.Collections.Generic; class Example { static void Main() { try { …
Lucian
  • 3,981
  • 5
  • 30
  • 34
150
votes
11 answers

Distinct not working with LINQ to Objects

class Program { static void Main(string[] args) { List books = new List { new Book { Name="C# in Depth", Authors = new List { …
Tanmoy
  • 44,392
  • 16
  • 45
  • 55
119
votes
7 answers

How to use the IEqualityComparer

I have some bells in my database with the same number. I want to get all of them without duplication. I created a compare class to do this work, but the execution of the function causes a big delay from the function without distinct, from 0.6 sec to…
Akrem
  • 5,033
  • 8
  • 37
  • 64
79
votes
8 answers

Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

I happened to have seen some code where this guy passed a lambda expression to a ArrayList.Sort(IComparer here) or a IEnumerable.SequenceEqual(IEnumerable list, IEqualityComparer here) where an IComparer or an IEqualityComparer was expected. I can't…
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
70
votes
4 answers

How to implement IEqualityComparer to return distinct values?

I have a L2E query that returns some data that contains duplicate objects. I need to remove those duplicate objects. Basically I should assume that if their IDs are the same then the objects are duplicate. I've tried q.Distinct(), but that still…
Bogdan Verbenets
  • 25,686
  • 13
  • 66
  • 119
65
votes
5 answers

IEqualityComparer that uses ReferenceEquals

Is there a default IEqualityComparer implementation that uses ReferenceEquals? EqualityComparer.Default uses ObjectComparer, which uses object.Equals(). In my case, the objects already implement IEquatable, which I need to ignore and…
Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
63
votes
6 answers

What problem does IStructuralEquatable and IStructuralComparable solve?

I've noticed these two interfaces, and several associated classes, have been added in .NET 4. They seem a bit superfluous to me; I've read several blogs about them, but I still can't figure out what problem they solve that was tricky before .NET…
thecoop
  • 45,220
  • 19
  • 132
  • 189
38
votes
5 answers

EqualityComparer.Default vs. T.Equals

Suppose I've got a generic MyClass that needs to compare two objects of type . Usually I'd do something like ... void DoSomething(T o1, T o2) { if(o1.Equals(o2)) { ... } } Now suppose my MyClass has a constructor that supports…
takrl
  • 6,356
  • 3
  • 60
  • 69
36
votes
2 answers

Linq Except with custom IEqualityComparer

I am trying to find the difference between two generic lists, as in the example below. Even though t1 and t2 contain the same properties, they are not the same object, so I have need to implement an IEqualityComparer. This appears to be working…
sgmoore
  • 15,694
  • 5
  • 43
  • 67
33
votes
3 answers

IEqualityComparer for anonymous type

I have this var n = ItemList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor, s.Description, s.Invoice }).ToList(); n.AddRange(OtherList.Select(s => new { s.Vchr, s.Id, s.Ctr, s.Vendor, s.Description, s.Invoice }).ToList();); I would like to do…
kjgilla
  • 1,227
  • 3
  • 12
  • 13
26
votes
2 answers

IEqualityComparer for SequenceEqual

In C#, is there an IEqualityComparer that uses the SequenceEqual method to determine equality?
user2037593
25
votes
4 answers

What is the difference between using IEqualityComparer and Equals/GethashCode Override?

When i am using dictionaries sometimes I have to change the default Equals meaning in order to compare Keys. I see that if I override the Equals and GetHashCode on the key's class or i create a new class which implements IEqualityComparer I have…
dariush624
  • 343
  • 1
  • 4
  • 9
19
votes
8 answers

C# 3.0: Need to return duplicates from a List<>

I have a List<> of objects in C# and I need a way to return those objects that are considered duplicates within the list. I do not need the Distinct resultset, I need a list of those items that I will be deleting from my repository. For the sake of…
SaaS Developer
  • 9,835
  • 7
  • 34
  • 45
19
votes
3 answers

linq Except and custom IEqualityComparer

I'm trying to implement a custom comparer on two lists of strings and use the .Except() linq method to get those that aren't one one of the lists. The reason I'm doing a custom comparer is because I need to do a "fuzzy" compare, i.e. one string on…
Joe
  • 565
  • 2
  • 4
  • 14
1
2 3
18 19