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

Grouping by IEnumerable does not work at all

I'm not really sure, why grouping by IEnumerable does not work. I provide custom IEqualityComparer, of course. public class StringCollectionEqualityComparer : EqualityComparer> { public override bool…
mnn
  • 1,952
  • 4
  • 28
  • 51
0
votes
1 answer

Implementing comparer for collection of collections

I have my class MyClass> : ICollection> I have data stored in: Dictionary> Data and I want to implement IEqualityComparer for my Data.…
Miloš Lukačka
  • 842
  • 1
  • 11
  • 25
0
votes
1 answer

c# implementing IEqualityComparer for generic class T

Is there any way of implementing IEqualityComparer for generic class? I tried: public class MyComparer : IEqualityComparer which is wrong, because MyGenericClass takes 3 arguments as a generics, so next one public class MyComparer :…
Miloš Lukačka
  • 842
  • 1
  • 11
  • 25
0
votes
2 answers

ObservableCollection.Distinct() is not working

I have the next class: public class MapsDescModel : NotificationObject, IEqualityComparer { public MapsDescModel(ObservableCollection mainCategoty) { MainCategories = mainCategoty; } private…
Ofir
  • 5,049
  • 5
  • 36
  • 61
0
votes
1 answer

ReSharper not displaying properties from underlying class when creating equality comparers

My setup is the following: I have POCO classes which are generated by T4 templates based off of my database. My domain models inherit from these POCOs. Basically the POCO models are there so that when I change the database I don't have to update the…
Marko
  • 12,543
  • 10
  • 48
  • 58
0
votes
1 answer

IEqualityComparer instance for two-dimensional arrays

F# supports structural equality of two-dimensional arrays with the = operator, and in F# collections such as Set. But how can I use the same equality comparison in the .NET class HashSet? By default it uses referential equality, and although there…
Todd Owen
  • 15,650
  • 7
  • 54
  • 52
0
votes
1 answer

Hashsets and different instances of class with all members identical

Let's say I have a class called myclass. In my code I have two instances of myclass, myclass1 and myclass2. Everything about them is (public and private) properties are identical. If I try to add both of them to a HashSet will it add both or only…
Matt
  • 25,943
  • 66
  • 198
  • 303
0
votes
1 answer

Checking instance of non-class constrained type parameter for null in generic method

I currently have a generic method where I want to do some validation on the parameters before working on them. Specifically, if the instance of the type parameter T is a reference type, I want to check to see if it's null and throw an…
casperOne
  • 73,706
  • 19
  • 184
  • 253
0
votes
2 answers

Is there a way to find an object's properties in List using Contains?

I was wandering how can I find out if an object already exists in my List. I'm adding "newPerson" (instance of Person class) in a List, but checking if newPerson contents/properties exists or not in the List. This piece works fine: …
Junior Mayhé
  • 16,144
  • 26
  • 115
  • 161
0
votes
4 answers

Filtering Duplicate items in an array based on child array in c#

i have an list of persons with basecode and an array of locations. i need to eliminate the persons in the list having different basecode with same locations and keep persons with differend locations. i tried using IEqualityComparer, and group by in…
Vilsad P P
  • 1,529
  • 14
  • 23
0
votes
2 answers

Specify equality with a lambda expression

I want to compare 2 collections. One of these is a List and the other is a List. Book has a Isbn property of type string, and I want write something like that : List isbnBooks= new List {"978-1933988276",…
Florian
  • 4,507
  • 10
  • 53
  • 73
0
votes
1 answer

Using IEqualityComparer to check specific values

So this is the first time I've tried using IEqualityComparer and I'm running into an issue. It's likely I just don't understand exactly what the code is doing behind the scenes. The list I'm providing it looks like this: Test Run | SN | Retest 1 …
static416
  • 1,759
  • 1
  • 11
  • 22
-1
votes
1 answer

Best way to compare 2 Data models in C# .NET Core

I have 2 data models with same properties which I gets data from 2 web API responses. I am trying to compare 2 models with values, If data difference is found, I need to compare ,find differences if found assign to a new instance of data model or…
PavanKumar GVVS
  • 859
  • 14
  • 45
-1
votes
1 answer

Shortest function that gives same result as === when comparing 2 variables

This is my take on it. Is it possible to make it shorter? (Not just by turning it into an arrow function) Must not use any === this is my third attempt, I really don't know now if its possible to shorten it evem further function strictEquals(a, b)…
-1
votes
1 answer

Correct way to implement IEqualityComparer

I'm currently working on a .net 5.0 application. I need to implement an IEqualityComparer. What is the correct way, to implement this interface and prevent NullRefrence Exceptions? My class to compare looks like this: public class Fruit { …
azzurro123
  • 521
  • 2
  • 10
  • 19
1 2 3
18
19