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
4
votes
2 answers

List.Except(List) not working as expected

I have two lists of the same type. One list is a deserialized json object loaded from a file and casted to the correct type. The other is a list created of objects of the same type. When I do List2.Except(List1) I expect to see everything in List2…
5SK5
  • 159
  • 1
  • 1
  • 15
4
votes
4 answers

List.Contains is not working as hoped

If I have an object of type MyBull and a List orig: // Just an example MyBull x = getMeTheObjectWithIdFromDB(9); orig.add(x); // Again same? data object MyBull y = getMeTheObjectWithIdFromDB(9); Why is this false then? // This is false,…
VoodooChild
  • 9,776
  • 8
  • 66
  • 99
4
votes
1 answer

Intersect two generic lists by dynamic properties

i have two generic lists with a few properties to compare but i want that the key identifiers are dynamic by a List. So lets say we have the class: class A { string Name { get; set; } string Color1 { get; set; } string Color2 {…
kassi
  • 358
  • 1
  • 3
  • 10
4
votes
2 answers

IEqualityComparer using list of string as comparer

I'm attempting to setup an IEqualityComparer that uses a list of string as the comparing property. When using Except and Intersect in the 2 lines of code below, all records are seen as 'new' and none are recognized as…
mrb398
  • 1,277
  • 4
  • 24
  • 32
4
votes
3 answers

Is there any built in collection type or IEqualityComparer for collection which bases equality on the items in it?

Is there any built in collection type (IEnumerable) or IEqualityComparer for an IEnumerable in the framework that has it's Equals (and GetHashCode accordingly) defined by the equality of the items in it? Something like: var x = new…
nawfal
  • 70,104
  • 56
  • 326
  • 368
4
votes
1 answer

Unit test GetHashCode with developer time in mind

In my current project I have several IEqualitycomparers. These take several properties of an object and compare them. Properties can be either equal, different and this both for values and null. I want to unit test these, but all the different…
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
3
votes
3 answers

IEqualityComparer with Linq to XML and Distinct() is not executed in code?

It doesnt matter what I write in the Equals method. The GetHashCode is always fired, but I do not know whose GetHashCode to return? When the GetHashCode method is called then variable x has the following data: In the first unitName elementName is…
msfanboy
  • 5,273
  • 13
  • 69
  • 120
3
votes
1 answer

How to get raw hash code from a class that re-implements GetHashCode?

Short question: How do I get the object.GetHashCode() value for an object that has re-implemented GetHashCode()? Long story: So I have about a hundred thousand objects, each sharing many (non-compile time) common strings. Common as in if the value…
Mania
  • 1,718
  • 14
  • 16
3
votes
2 answers

Strange behavior of EqualityComparer with nullable fields

Assume there is this class: public class Foo { public int Id { get; set; } public int? NullableId { get; set; } public Foo(int id, int? nullableId) { Id = id; NullableId = nullableId; } } I need to compare these…
Yurii
  • 33
  • 4
3
votes
2 answers

Enumerable.SequenceEqual and EqualityComparer

From MSDN The SequenceEqual(IEnumerable, IEnumerable) method enumerates the two source sequences in parallel and compares corresponding elements by using the default equality comparer for TSource, Default. The default equality…
flockofcode
  • 1,799
  • 2
  • 12
  • 21
3
votes
4 answers

Can we compare two complex collections field by field using IEqualityComparer interface using LINQ extension method SequenceEqual

I'm trying to use IEqualityComparer to compare 2 fields from 2 collections field by field. IEqualityComparer is comparing only 1 field "name". I want to compare "mark" as well. In Java, we have comparator interface to compare more than one fields in…
3
votes
1 answer

Neither Equals nor GetHashCode get called on IEqualityComparer

I'm comparing two List> with my own IEqualityComparer> implementation, but neither GetHashCode nor Equals method get called. Here's my own IEqualityComparer implementation. public class…
devunt
  • 347
  • 3
  • 9
3
votes
2 answers

Use IEqualityComparer to generate a distinct list based on two properties in C#

I am trying to generate a listbox of items that is a concatenation of two strings. I have created a class that implements IEqualityComparer and I want to make this list distinct. private void PopulateFamily() { var source =…
ehh
  • 3,412
  • 7
  • 43
  • 91
3
votes
3 answers

How to use a float as key in c# Dictionary with custom comparer that rounds to nearest .01?

I'm looking to implement an IEqualityComparer class that stores and compares floating point keys that are rounded to the nearest 0.01. In particular, I want to make sure I implement the GetHashCode method correctly. I would like to make this as…
Roci
  • 113
  • 3
  • 10
3
votes
3 answers

IEqualityComparer string value within object

I quite possibly am doing this the wrong way but; I have a list of objects in LINQ; MyObj string name string somethingElse List myObjects; Now I'm trying to see if any object in that list has a string value; So I have; if…
griegs
  • 22,624
  • 33
  • 128
  • 205