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
1
vote
2 answers

C#: Use unsafe code to compare a variable to default(T)

I want to test whether a variable of unknown type has been assiged a non-default value. The variable is probably a struct type, so I can't solve this with where T : class. The struct's IEquatable implementation will usually assume that its fields…
user6629955
1
vote
3 answers

Abstract an IEqualityComparer implementation or override the default comparer to use Distinct method

I'm trying to find a distinct List given a List where each BlogPost has an Author property. I've found the Distinct() extension method in generics and I'm trying to use it. First, let me explain my loop and where I want to use it,…
Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
1
vote
1 answer

How do I override GetHashCode() without any numbers as fields?

All of the resources showing how to override Equals(object) and GetHashCode() use numeric fields to implement the GetHashCode() method: Implementing the Equals Method What's the best strategy for Equals and GetHashCode? Why is it important to…
Evorlor
  • 7,263
  • 17
  • 70
  • 141
1
vote
0 answers

changing ReSharper Template for IEquatable and IEqualityComparer

Is there a way to change the implementation details, when you add a IEquatable or IEqualityComparer with ReSharper to your own class? I am using R# Ultimate 2016.3 I want to have some brackets around single if statement, because of StyleCop…
Dom84
  • 852
  • 7
  • 20
1
vote
1 answer

Linq compare two ObservableCollection with Except

I have read about the IEqualityComparer interface. Here is my code (which says more then a thousand words) static void Main(string[] args) { var Send = new ObservableCollection() { new ProdRow() { Code = "8718607000065",…
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
1
vote
2 answers

Why can I not use customer.Name.contains("smith") in IEqualityComparer Equals method

I want to use the HashSet.Contains method because its super fast. var hashset = new HashSet(customers, new CustomerComparer()); var found = hashset.Contains(new Customer{ Id = "1234", Name = "mit" }); // "mit" instead of an equals "smith"…
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
1
vote
1 answer

EqualityComparer GetHashCode and Equals

i have a Dictionary of types (Dictionary) with a custom comparer, because we want to store relationships between 2 given types (for a MVVM pattern), and i need help comming up with a way to get custom EqualityComparer to work. Doing some research i…
Loucry
  • 23
  • 4
1
vote
6 answers

C# collection of two points doesn't return any results

Class: public class Point : IEqualityComparer { public char HorizontalPosition { get; set; } public int VerticalPosition { get; set; } public Point(char horizontalPosition, int verticalPosition) { …
FrenkyB
  • 6,625
  • 14
  • 67
  • 114
1
vote
2 answers

Union Lists using IEqualityComparer

I'we got two Lists of my class Nomen: var N1 = new List(); var N2 = new List(); public class Nomen { public string Id; public string NomenCode; ... public string ProducerName; public decimal? minPrice; } I…
el_konor
  • 528
  • 1
  • 6
  • 15
1
vote
2 answers

Checking equality with a HashSet of objects

I am trying to compare two hashsets of Definition type as EqualityComparer.Default.Equals(value, oldValue). Definition is defined as follows public class Definition { public string Variable { get; set; } public HashSet
Sreeja
  • 35
  • 1
  • 1
  • 7
1
vote
2 answers

Inline Comparer

I have a class Person with a Name property. I have a collection of persons. I have a method to add a new person but I need to check of the collection already contains the person. I would like to use coll.Contains(newPerson,[here is the comparer])…
ehh
  • 3,412
  • 7
  • 43
  • 91
1
vote
1 answer

IEqualityComparer not working for 'Contains' method

Okay, so I have the following classes/interfaces FilterFileViewModel, CategoryViewModel, IFilterViewModel, ICategoryViewModel. Inheritance is set up as follows: IFilterViewMode : IEqualityComparer ICategoryViewModel :…
jimbo
  • 603
  • 9
  • 27
1
vote
1 answer

LINQ Distinct with custom IEqualityComparer

So, I have a class like this: public History { int ProcessedImageId; string UserId; DateTime TimeStamp; ... } From a LINQ query, I am getting every "History" within a range of time. Now, I am also executing a second query to…
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
1
vote
1 answer

IEqualityComparer does not work with List.Distinct() method

I have the following simple object: public class Net : IEqualityComparer { public string Name { get; private set; } public int Id { get; set; } private Gate _inGate; private Gate _outGate; private NetValue _value =…
Dumbo
  • 13,555
  • 54
  • 184
  • 288
1
vote
1 answer

IEqualityComparer custom implementation and set-operations

i need to perform simple set-operations in linq (for example Union, Except and Intersect) class Person { public int Id { get; set; } public string Name { get; set; } public Person() { } public Person(int id, string…
Ivan Stelmakh
  • 195
  • 1
  • 6