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

How to iterate only distinct string values by custom substring equality

Similar to this question, I'm trying to iterate only distinct values of sub-string of given strings, for example: List keys = new List() { "foo_boo_1", "foo_boo_2, "foo_boo_3, "boo_boo_1" } The output for the…
Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91
2
votes
4 answers

Compare two lists that contain a lot of objects (2th part)

Referring to the question that I previously asked: Compare two lists that contain a lot of objects It is impressive to see how fast that comparison is maide by implementing the IEqualityComparer interface: example here As I mentioned in my other…
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
2
votes
2 answers

If getHashCode() for string or integer is not guaranteed to be unique why use it?

As i wrote in the title. If its not safe to use getHashCode() in your application, why use it? (for string and integer) I want to use it to intersect methods and except metods in Linq models or create my own IEqualityCompare class. It feels like a…
Niklas
  • 1,753
  • 4
  • 16
  • 35
2
votes
0 answers

IEquatable with GetHashCode?

I have created a Graph Class including HashSet as Nodes and HashSet> as Edges. public interface IEdge { T A_Node { get; } T Another_Node { get; } } public interface IGraph { ISet Nodes { get; } ISet>…
Parsa
  • 7,995
  • 2
  • 27
  • 37
2
votes
1 answer

How to use a custom comparer for EqualityComparer.Default?

Note: my case is for byte[] but I believe a good answer would work for any type. Visual Studio's auto-generated implementation of Equals uses EqualityComparer.Default.Equals(T x, T y) for reference types. I have a lot of classes with byte arrays…
0xFF
  • 808
  • 1
  • 12
  • 33
2
votes
3 answers

How to find overlapping in collection using LINQ ( not duplicates but to find overlaps)

List items = new List(); items.Add(new StoreDetailDto { StoreNumber = 2, WarehouseNumber = 4201, IsResolved = false, StoreName = "StoreEx", WarehouseName = "WarehouseEx1"}); items.Add(new StoreDetailDto { StoreNumber…
VR1256
  • 1,266
  • 4
  • 28
  • 56
2
votes
2 answers

Linq Distinct with a single comparison class (and interface)

I have several classes in my application, all of which have a Name property that I want to use as my basis for comparison (Distinct(), etc.). Since I am always going to be comparing on Name, I decided to extract an interface, ISomeComparedStuff,…
Brian David Berman
  • 7,514
  • 26
  • 77
  • 144
2
votes
1 answer

Proper way to write GetHashCode() when Equality Comparer is based on OR operation?

I'm trying to write an Equality Comparer for a simple class with 3 fields, like so: public class NumberClass { public int A { get; set; } public int B { get; set; } public int C { get; set; } } My condition for two objects of…
Sach
  • 10,091
  • 8
  • 47
  • 84
2
votes
2 answers

Comparison Operator using Reflection

I want to compare two values at runtime using reflection. I was using Comparer.Default.Compare(x,y) for this, but I have come to realize that this is not adequate. Let's say I want to compare a double to a single (1.0 == 10). Comparer.Default will…
Jeff
  • 35,755
  • 15
  • 108
  • 220
2
votes
0 answers

IEqualityComparer to compare two complex objects

Referencing my previous question Compare two complex list objects I have implemented IEqualityComparer to check if my list of objects are equal, but it doesn't seem to work. Can anyone help find the issue public class Compare :…
Arti
  • 2,993
  • 11
  • 68
  • 121
2
votes
2 answers

How does Linq Except Compare results

How does Except determine if 2 values are the same I have the following code var removes = collection.Except(values, comparer).ToList(); var adds = values.Except( collection, comparer).ToList(); foreach (var item in removes) { …
MikeT
  • 5,398
  • 3
  • 27
  • 43
2
votes
1 answer

Removing duplicate byte[]s from a collection

This will probably be an extremely simple question. I'm simply trying to remove duplicate byte[]s from a collection. Since the default behaviour is to compare references, I tought that creating an IEqualityComparer would work, but it doesn't. I've…
GameZelda
  • 824
  • 1
  • 7
  • 13
2
votes
1 answer

IEqualityComparer to use Except, Intersect

I'm attempting to implement IEqualityComparer so I can compare custom objects for equality and differences. Scenario: I have 10 batches of record sets that were imported at different times. I need to look at the most recent record set, and compare…
mrb398
  • 1,277
  • 4
  • 24
  • 32
2
votes
3 answers

Which IEqualityComparer is used in a Dictionary?

Lets say I instantiate a dictionary like this var dictionary = new Dictionary(); And MyClass is my own class that implements an IEqualityComparer<>. Now, when I do operations on the dictionary - such as Add, Contains,…
2
votes
3 answers

Join + IEqualityComparer and HashCode

Im writing my own LINQ reference but Im getting troubles with some of the more complicated operators implementations. There is a Join implementation that takes a IEqualityComparer Im getting just crazy. Im trying to understand it first before I…
Jesus Rodriguez
  • 11,918
  • 9
  • 64
  • 88