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

What's a good place to put Comparers?

EqualityComparer...where should it go? Nested in the class it's comparing? Or in it's own file? Or in a file with all the other custom Comparers? Are there generally agreed upon coding guidelines, and what do they suggest in this case?
richard
  • 12,263
  • 23
  • 95
  • 151
1
vote
1 answer

Difference between two overloads of Enumerable.Except?

I am trying to understand the difference between two overloads of Enumerable.Except method i.e. Except(IEnumerable, IEnumerable) Except(IEnumerable, IEnumerable, IEqualityComparer) Obviously, the first differnce is that the first overload uses…
Coder23
  • 128
  • 8
1
vote
1 answer

UnitTesting List of custom objects with List of custom objects for equality

I'm writing some UnitTests for a parser and I'm stuck at comparing two List where T is a class of my own, that contains another List. My UnitTest compares two lists and fails. The code in the UnitTest looks like…
citronas
  • 19,035
  • 27
  • 96
  • 164
1
vote
1 answer

Enumerable.Except with IEqualityComparer

I have two string arrays, newArray and oldArray, and I want to use Enumberable.Except method to remove all items that are in newArray that are also in oldArray and then write the result to a csv file. However, I need to use a custom comparer in…
Mikkel Bang
  • 574
  • 2
  • 13
  • 27
1
vote
1 answer

HashSet.IntersectWIth using a custom IEqualityComparer produces wrong results in Mono

I'm having in issue where a certain piece of code is running as expected in .NET 4.0 but not in Mono 2.6 (in Unity3D). Please have a look: void Test() { Func get = name => typeof(HSTest).GetField(name,…
vexe
  • 5,433
  • 12
  • 52
  • 81
1
vote
1 answer

How to search the same object with different Equal concepts?

I have several objects that depending on the use case are considered Equal differently. I need to use these objects as keys for dictionaries and as far as I know Dictionary<> use the Equals() method which limits me to have only one implementation of…
Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
1
vote
1 answer

POCO comparer not working

I have some code that allows me to compare to Entity Framework entities. However, I've noticed that it sometimes returns false (not matched) when it is in fact true (from a logical perspective). It fails because of the HashSets - they always return…
dotnetnoob
  • 10,783
  • 20
  • 57
  • 103
1
vote
2 answers

Compare Two Datatable that has same schema and N no of columns

I have two DataTables both has same no of columns and column names.Am in need of comparing both for the different rows.Which means even if one cell doesnt match the row should be plotted.I tried with table1.Merge(table2); DataTable modified =…
Dhivya Sadasivam
  • 155
  • 2
  • 2
  • 14
1
vote
1 answer

What is the proper way to set up a always false IEqualityComparer?

I have a case where two objects can be compared many different ways for equality. For example: public class HeightComparer : IEqualityComparer { public bool Equals(Person x, Person y) { return x.Height.Equals(y.Height); } …
Moop
  • 3,414
  • 2
  • 23
  • 37
1
vote
2 answers

Dictionary using is custom key but key is always unequal

I am using RTBTextPointer as custom key in dictionary... Init.SpintaxEditorPropertyMain.SpintaxListDict = new Dictionary(new RTBTextPointerComparer()); I worte this RTBTextPointer, and…
Jay
  • 33
  • 5
1
vote
2 answers

Except compares items from calling (first) collection

I'm using Enumerable.Except() to exclude skipSerialNumbers items from activatedSerialNumbers. activatedSerialNumbers = activatedSerialNumbers .Except(skipSerialNumbers, new…
Zharro
  • 819
  • 1
  • 11
  • 23
1
vote
1 answer

Is there a built-in IEqualityComparer that compares objects only using their hash value?

Is there a built-in IEqualityComparer that compares objects by the value returned by their GetHashCode value? It's easy to write, but I'd prefer to use a provided class instead of a custom one. Current code: private class HashComparer :…
mafu
  • 31,798
  • 42
  • 154
  • 247
1
vote
2 answers

Compare two lists, and get all differences?

I have to compare two lists of type Slide which contain another List of Items, called Charts. I have to find all differences between the Slide-lists, whereby a difference could be: a Slide in List A , but not in B a Slide in List A , but not in B a…
Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
1
vote
2 answers

IEquatable on POCO identity field

I have POCOs from a SQL Server database that have an identity ID field. I would like to implement IEquatable so I can check if they're the same record, use .Contains() on List etc. Assuming I will never need to compare unsaved instances, is it…
Sean
  • 14,359
  • 13
  • 74
  • 124
1
vote
4 answers

How should I implement IEqualityComparer.Equals

Concerning IEqualityComparer, is there ever a reason why the Equals(T x, T y) implementation should be anything other than what I have below? public class MyEquality : IEqualityComparer { //My Equals(T x, T y) always looks like this …
user2023861
  • 8,030
  • 9
  • 57
  • 86