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

IEqualityComparer fails using two properties

When I use this comparer in Distinct() it always returns false. Can't see a reason why. public class IdEqualityComparer : IEqualityComparer { public bool Equals(Relationship x, Relationship y) { if (x == null && y ==…
Paul Keefe
  • 44
  • 3
-1
votes
1 answer

Comparing two lists of class objects similar to a Diff Tool

Question moved here. My requirement is to write a program that sort of mimics diff tools. Yes there are quite a few libraries and open source code that accomplishes this purpose, but I would like to write my own comparer. Here's the starting point.…
Sach
  • 10,091
  • 8
  • 47
  • 84
-1
votes
4 answers

Determining equality for unknown types

I need to check the given instance is matching with the collection (Both are unknown types). Have a look void Main() { // Employee "John" Object got from Service Layer #1 Object obj1 = Client1.GetObject("John"); // Employee "John"…
user6060080
-1
votes
9 answers

what's faster - the if statement or a call of a function?

i'm writing an interesting program, and every performance hit is very painful for me. so i'm wondering what is better - to make an extra "if" statement to reduce a number of function calls, or to avoid those "if" adn get more funciton calls. the…
Mark
  • 55
  • 2
-1
votes
1 answer

IEqualityComparer for class with many properties neither unique value

How do implementation for IEqualityComparer for this class? The ID property is not unique. Neither properties has unique values. The entity has 7 properties. [Serializable()] public class ServidorSeleccionadoDto { [XmlAttribute()] public int…
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
-1
votes
3 answers

What interfaces must I implement to make a List or Dictionary concatenate two values as a key

I need to make my custom object work correctly in a Dictionary, List, etc... so that I can change properties of the object, and allow it to be resorted, and not orphaned. The last time I attempted overriding GetHashCode(), I orphaned objects when I…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
-2
votes
3 answers

IEqualityComparer GetHashCode return same values when items are not equal?

In the "Notes to Implementers" section in the documentation for the GetHashCode method of the IEqualityComparer interface, it states: Implementations are required to ensure that if the Equals method returns true for two objects x and y, then…
casperOne
  • 73,706
  • 19
  • 184
  • 253
-2
votes
3 answers

Not getting properties on generic parameter T when trying to create generic comparer to compare 2 objects

I want to compare 2 objects like below : I don't want 2 employees in the same department I don't want 2 Animals in the same Zoo So on..... I am trying to implement IEqualityComparer to accept generic type argument to take Employee or Animals or…
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
-2
votes
3 answers

Using custom EqualityComparer to check if C# item has been updated in a list

I am comparing 2 lists of objects using a custom comparer like so: public class LocationEqualityComparer : IEqualityComparer { public bool Equals(LocationData x, LocationData y) { var idComparer = string.Equals(x.Id,…
Jordan1993
  • 864
  • 1
  • 10
  • 28
-2
votes
4 answers

Equals, GetHashCode and Operators override not being called in IEquatable implementation

I'm trying to implement IEqueatable so I could use .Except in my custom type LINQ queries. The custom type code looks like this: public class Case : IEquatable { [Key] public int Id { get; set; } //More…
RainierMallol
  • 806
  • 1
  • 8
  • 24
-2
votes
2 answers

C# SynchronizedReadOnlyCollection.Contains ( T, IEqualityComparer )

When I call: SynchronizedReadOnlyCollection.Contains ( T, IEqualityComparer ) it fails to call: IEqualityComparer.Equals(T x, T y) I have sample code…
1 2 3
18
19