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
16
votes
3 answers

Using IEqualityComparer for Union

I simply want to remove duplicates from two lists and combine them into one list. I also need to be able to define what a duplicate is. I define a duplicate by the ColumnIndex property, if they are the same, they are duplicates. Here is the approach…
Matthew Cox
  • 13,566
  • 9
  • 54
  • 72
15
votes
3 answers

IEqualityComparer not working as intended

I have a List of paths of files stored on my computer. My aim is to first filter out the files which have the same name and and then filter out those which have the same size. To do so, I have made two classes implementing IEqualityComparer,…
Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97
13
votes
2 answers

writing a custom comparer for linq groupby

Again this sample is a very simplified version of my actual problem involving a custom comparer for linq grouping. What have I done wrong? The code below produces the result below (1.2, 0), (4.1, 0), (4.1, 0), (1.1, 0), however I was expecting the…
DustyB
  • 145
  • 1
  • 1
  • 8
13
votes
1 answer

Checking for equality in Objective-C

How do i check the key in dictionary is same as the string in method parameter? i.e in below code , dictobj is NSMutableDictionary's object , and for each key in dictobj i need to compare with string. How to achieve this ? Should i typecase key to…
suse
  • 10,503
  • 23
  • 79
  • 113
13
votes
2 answers

Implementing IEqualityComparer on an object with two properties in C#

I have a case where I need to grab a bunch of items on distinct, but my source is a collection of objects with two properties, like this: public class SkillRequirement { public string Skill { get; set; } public string Requirement { get; set;…
Felix Weir
  • 459
  • 7
  • 18
12
votes
3 answers

How to use linq `Except` with multiple properties with different class?

I am trying to learn the Linq/Lambda expressions and was stuck at somewhere. What I was Doing I have created two classes with properties which have some common properties in them. The classes are like(It's test code). class TestA { …
user2745246
  • 304
  • 1
  • 3
  • 14
11
votes
1 answer

IEqualityComparer vs EqualityComparer?

I've read this post but it doesn't answer my question. MSDN says: We recommend that you derive from the EqualityComparer(Of T) class instead of implementing the IEqualityComparer(Of T) interface, because the EqualityComparer(Of T) class tests for…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
11
votes
2 answers

IEqualityComparer with a tolerance; how to implement GetHashCode?

I'm implementing a reusable DoubleEqualityComparer (with a custom tolerance: the "epsilon" constructor parameter) to ease the usage of LINQ with sequences of double. For example: bool myDoubleFound = doubles.Contains(myDouble, new…
Notoriousxl
  • 1,540
  • 1
  • 16
  • 27
9
votes
1 answer

KeyedCollection String Case Insensitive

Tried following the documentation and I cannot make it work. Have a KeyedCollection with the key string. How to make the string key case insensitive in a KeyedCollection? On a Dictionary can just pass StringComparer.OrdinalIgnoreCase in the…
paparazzo
  • 44,497
  • 23
  • 105
  • 176
8
votes
1 answer

Linq IEqualityComparer Ignore Case

I am sorting a list of elements: var matchEle = listOfElements.Where(e => e.Properties().Any(p => p.Name.Contains("Key", Asking for IEqualityComparer))).First(); I am used to just going straight to a StringComparer, OrdinalIgnoreCase or…
AndyBernard
  • 105
  • 1
  • 9
8
votes
1 answer

EqualityComparer.Default isn't clever enough

I was reading the source code of EqualityComparer.Default and found that it's not so clever. Here is an example: enum MyEnum : int { A, B } EqualityComparer.Default.Equals(MyEnum.A, MyEnum.B) //is as fast as…
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
8
votes
4 answers

Why IEqualityComparer has GetHashCode() method?

IEqualityComparer in the namespace System.Collections.Generic has following methods: bool Equals(T x, T y); int GetHashCode(T obj); Since this inteface is used to check equality of objects, the first method Equals makes sense. But why do we need to…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
8
votes
3 answers

How to Implement IEqualityComparer With Tolerance

This question is similar to the one here. We all know what PointF is, don't we? This is the data structure: public struct PointF { public float X; public float Y; } How to implement IEqualityComparer with tolerance? Let's say my Equals…
Graviton
  • 81,782
  • 146
  • 424
  • 602
8
votes
3 answers

Hashtables (Dictionary etc) with integer keys

I've been puzzling over this for a few days... feel free to shoot down any of my assumptions. We're using a Dictionary with integer keys. I assume that the value of the key in this case is used directly as the hash. Does this mean (if the keys are…
spender
  • 117,338
  • 33
  • 229
  • 351
7
votes
5 answers

Comparing two lists and ignoring a specific property

I have two employee lists that I want to get only unique records from but this has a twist to it. Each list has an Employee class in it: public class Employee { // I want to completely ignore ID in the comparison public int ID{ get; set; } // I…
Frekster
  • 1,138
  • 1
  • 14
  • 32
1
2
3
18 19