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

Does it make sense to call an EqualityComparer with an interface as generic parameter

Let's say I have an this code: public interface IInterface { // Properties... } internal class Realisation : IInterface, IEquatable { // Properties... public bool Equals(IInterface other) { // ... } …
gfache
  • 606
  • 6
  • 15
0
votes
1 answer

Unity Engine - KeyNotFound Exception

So, I've trying to make a 2D roguelike in Unity, I want to implement a pixel shadow based on sprites (I have a tile set containing all shadow tiles I may need), So I made that each Shadow had a "shadowType", to define either the shadow is coming…
0
votes
1 answer

generic reference equality comparer does not work for ValueTuple

I have written a generic equality comparer that should always compare by reference, no matter how the GetHashCode and Equals methods of the parameter type look like: public class ReferenceEqualityComparer : IEqualityComparer { public…
Kjara
  • 2,504
  • 15
  • 42
0
votes
1 answer

Unable to debug GetHashCode method

I have implemented a equality comparer in below manner. class BoxEqualityComparer : IEqualityComparer { public bool Equals(Box b1, Box b2) { if (b2 == null && b1 == null) return true; else if (b1 == null | b2…
Power Star
  • 1,724
  • 2
  • 13
  • 25
0
votes
1 answer

Bitwise Or of null check in C#

I was going through the IEqualityComparer Example here I don't understand the need for the second condition with Bitwise Or if (b2 == null && b1 == null) return true; else if (b1 == null | b2 == null) return false; Why is there a need for…
rne18145
  • 1
  • 1
0
votes
1 answer

Delete priority for distinct

I have an object with many variables and i wanted a distinct function that would compare two variables(customerid,status) to consider the duplicates, i'm using the compare below for that, but I wish to choose the priority in how the distinct…
Carlos Siestrup
  • 1,031
  • 2
  • 13
  • 33
0
votes
0 answers

How to implement GetHashcode for a difference-tolerant DateTime comparer?

I've implemented an IEqualityComparer which can compare two dates based on a tolerance value in milliseconds. If the difference between the two is less than the tolerance, they should be treated as equal. The Equals side is almost trivial…
Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
0
votes
2 answers

Custom IEqualityComparer to get distinct objects from two lists faster than using .Where() + .Any()

so I've got two lists of objects, objects have multiple fields, but I'd like to distinct them basing on only two of them. To give you the picture, object KeyAndValue consists of fields Key and Tag, so: list1 = { obj1(key=1,tag=A), obj2(key=2,tag=A)…
0
votes
1 answer

Retrieve "identical" complex object from database

I have a table in LINQ to SQL called "Cars" that contains "Car" objects. Each Car has an EngineID and a ColourID. I have created a new, local Car object just in memory (not yet committed to the database). I already have an IEqualityComparer written…
Aaron
  • 1,802
  • 3
  • 23
  • 50
0
votes
1 answer

Using custom comparer for strings

I have following list: var ips = new List { "192.168.5.1", "192.168.0.2", "192.168.0.3", "192.168.0.4", "192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4" }.OrderBy(p => p.Ip); It looks like it works,…
newbie
  • 273
  • 2
  • 4
  • 13
0
votes
0 answers

GetHashCode Implemetation for multiple properties in class

I need to compare to List's of Gu45Entity List Gu45EntityItemsFromDb = DataBaseModel.Instance.Gu45DocumentStore.Include(x => x.EGu45.Gu45).ToList(); List temp =…
A191919
  • 3,422
  • 7
  • 49
  • 93
0
votes
2 answers

C# Remove Duplicates Only Checking on The First Element of The String Array

I have a list of string arrays. I want to remove duplicates and empty strings by doing a check only on the first element of the string array. I have seen some SO posts using IEqualityComparer to achieve removing duplicates comparing whole string…
Baz Guvenkaya
  • 1,482
  • 3
  • 17
  • 26
0
votes
1 answer

IEqualityComparer Not calling subclass in dictionary

I'm trying to implement an IEqualityComparer on a sub class which will be stored as a key to a dictionary. the following is what i have public class SuperClass : IEqualityComparer { public virtual bool Equals(SuperClass…
0
votes
2 answers

Distinct selection, even with a custom EqualityComparer, still leaves duplicate entries

I am fairly lost with this script - I don't get it - why does it leave duplicate entries? private static float GenerateMedian(IEnumerable items, KDAxis axis) { float[] allValues = items.SelectMany(AxisSelector(axis)).ToArray(); …
tomsseisums
  • 13,168
  • 19
  • 83
  • 145
0
votes
1 answer

Can't remove object from SortedDictionary C#

I have a node class which is contained in a SortedDictionary: SortedDictionary openList = new SortedDictionary(); I need to write the CompareTo method on the node, so that the nodes are sorted from lowest to highest based on…