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

why do i need GetHashcode() in the IEqualityComparer interface?

i see that the IEqualityComparer interface has Equals(obj x, obj y) and GetHashcode(). I fully understand the Equals() methods because i you have to say if two things are the same or not but why do i need to implement a GetHashCode() method as well?
leora
  • 188,729
  • 360
  • 878
  • 1,366
0
votes
0 answers

How to find an item on a generic List using IEqualityComparer?

I have a list of type List>. I need to check if this list has a TreeNode previosuly added. I use IndexOf for that purpose. I implement the Equals method on DepFile type. This does not work. The question is: How can I tell…
anmarti
  • 5,045
  • 10
  • 55
  • 96
0
votes
1 answer

GroupJoin overloaded with IEqualityComparer only compares objects in the inner collection

I have encountered some odd behaviour while implementing a Group Join with a customer IEqualityComparer. The following code demonstrates the behaviour that is the problem for me List inner = new List() { "i1", "i2" }; List
CurlyPaul
  • 1,138
  • 1
  • 10
  • 29
0
votes
4 answers

IEqualityComparer and weird results

Take a look at this class: public class MemorialPoint:IMemorialPoint,IEqualityComparer { private string _PointName; private IPoint _PointLocation; private MemorialPointType _PointType; private DateTime…
George Silva
  • 3,454
  • 10
  • 39
  • 64
0
votes
1 answer

IEqualityComparer in Union not comparing expected values

I'm trying to "fill" gaps in a set of time data with NAN for graphing purposes. I'm attempting to do this by creating a set of NAN data for all the possible time-periods and using a Union to merge them together. I'm hoping to have the real data…
Joe
  • 6,773
  • 2
  • 47
  • 81
0
votes
1 answer

Efficient way to group all equal objects defined by IEqualityComparer

I have a big list of certain objects that are of type Order. Furthermore, I have defined an IEqualityComparer and my goal is to efficiently create a list of lists (or groupings) where every sublist contains the objects that are equal to each…
T_D
  • 1,688
  • 1
  • 17
  • 28
0
votes
1 answer

NameValueCollection doesn't actually do anything with its IEqualityComparer?

I am looking at System.Collections.Specialized.NameValueCollection and it takes an IEqualityComparer, which is good news if someone like me wanted to sort the items in the collection by, say, something like the alphabetical order of the keys. But,…
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
0
votes
1 answer

Comparator/Sorting/Equatable methodology and return value

Alright so I'm taking everything I've learned and trying to implement it in C#. Given that I have a background in Java my ride has been pretty smooth so far, but I'm running into issues into using the Comparer object and functions etc. I don't care…
JonMcDev
  • 43
  • 1
  • 7
0
votes
1 answer

Equals Remove wrong assignment inside Equals

I have following class which i am using to compare some objects it looks like it: Imports System.Collections.Generic Public Class Part Implements IEqualityComparer(Of Part) Public _comparisonType As EqualsComparmission Public Sub…
unknown
  • 75
  • 7
0
votes
2 answers

64bit HashCodes, IEqualityComparer & Intersect/Except

I'm generating 64 bit hashcodes from strings, and storing this value in a database Is it possible to override GetHashCode with a 64 bit long type instead of 32 byte int? If this is not possible, is it possible to implement Equals and GetHashCode…
mrb398
  • 1,277
  • 4
  • 24
  • 32
0
votes
1 answer

Check if Dictionary contains key by comparing with different type

So what I want to do is this var result = dictionary.ContainsKey(Guid.Empty); Where dictionary is defined as var dictionary = new Dictionary(); Right now FooKeyClass is basically just some data with a public property of type…
Alex
  • 197
  • 1
  • 11
0
votes
2 answers

find elements of different types in two lists with common property value, linq variant of two for each loops

there are two lists Myobjects list contains all items, all have property "oid" of ObjectID The list of ObjectID types is list of ALL objects to be deleted Problem is to find any item in Myobjects that has ObjectID in oid property. I'm having a…
nekitip
  • 357
  • 1
  • 2
  • 10
0
votes
0 answers

What is the proper way to implement Equation functions

There seem to be a lot of equation interfaces: IEquatable, IEquatable, IEqualityComparer, IEqualityComparer. Which one should I implement to be able to detect if two objects have the same value (while my object decides what the same value…
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
0
votes
1 answer

Custom IEqualityComparer for HashSet and other Objects

I am using LINQ and doing a group by using multiple objects. One of these objects is a HashSet. var group = map.GroupBy(m => new{m.Item2.Clients,m.Item3,m.Item2.StartTimeID}); Where m.Item2.ClientCampaigns is of type HashSet, m.Item3 is a seperate…
ManJan
  • 3,939
  • 3
  • 20
  • 22
0
votes
2 answers

Equality Comparer using generic

I have a lot of List comparison, checked using sequenceEqual: this.Details.SequenceEqual(event.Details, new DetailsEqualityComparer()); Since in this way I have a lot of boilerplate, writing tons of class pretty similar (except for the…
BAD_SEED
  • 4,840
  • 11
  • 53
  • 110