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

Collections with user defined class - find differences

This is my model: public class EventModel { public DateTime? EVENT_DATE { get; set; } public decimal? EVENT_TYPE { get; set; } public string EVENT_DESCRIPTION { get; set; } } I fill this class into ObservableCollection. When…
Lucy82
  • 654
  • 2
  • 12
  • 32
0
votes
2 answers

Why when i call the remove operation of this hashset it doesn't use the gethashcode neither the equality implementation?

i am doing an app to manage the creation of role-playing sessions, but i am having problems with the section to do rules summaries so the Master doesnt have to be reading the core book every sec, i have the data structures in this way. User have a…
0
votes
1 answer

How to find a best match among combinations in C#

Say I have a collection which has 5 properties. My Input: A=1;B=2;c=3;d=4; Using this input i have to get the Property E ( which is exactly close to my input or nearly close to my input) In this case my expected result is Result A and Result C. So…
user3610920
  • 1,582
  • 2
  • 13
  • 24
0
votes
1 answer

Linq Union - IEqualityComparer and # of executions

Out of interest how does the GetHashCode of a concrete implementation of IEqualityComparer work? The reason that I ask is that I'm using linq to union two collections, and when only the left collection has an item GetHashCode is called twice.…
Tim Butterfield
  • 565
  • 5
  • 24
0
votes
1 answer

Create a generic method to use EqualityComparer that I have defined for specific types

I have many objects in which I want to check that lists of those objects are equal. For each object I have defined an EqualityComparer: public class BaseAssociatedEntity : BasePage { protected IWebElement EntityElement; protected virtual…
Hdot
  • 543
  • 1
  • 4
  • 15
0
votes
1 answer

Linq Group by Complex Object (E.G list element of object)

I have an object like below; var json = @"{ 'TableNumber': '3', 'TableId': '81872d39-9480-4d2d-abfc-b8e4f33a43b6', 'tableMenuItems': [ { 'Id': '4664a2d3-c0af-443d-8af5-2bd21e71838b', 'Name': 'Bonfile', …
Cemal
  • 153
  • 2
  • 8
0
votes
1 answer

GroupBy HashSet not grouping, whilst SetEquals is true

I have a situation where I need a collection to be GroupBy on a HashSet where myClass overrides Equals(myClass), Equals(object), GetHashCode(), ==, and !=. When I perform the GroupBy() the results are however not grouped. The same occurs…
Stefan de Kok
  • 2,018
  • 2
  • 15
  • 19
0
votes
1 answer

C#'s IEqualityComparer similar or alternative in Swift Language

I'm new in swift programming language and my background was .NET Development, I wonder is there any protocol (interface) or abstract class similar to C#'s IEqualityComparer in swift language? Note: I'm trying to developing SDK (I already had written…
bkulaksiz
  • 77
  • 1
  • 9
0
votes
0 answers

List> Except() List> Compare Lists of HashSets

I have two lists of HashSet and want to exclude one from the other. Should be able to achieve that using the Excepts extensions method. say List1 is [{1,2}, {2,3}] and List2 is [{2,3}] list1.Except(list2); should return [{1,2}] in my scenario.…
0
votes
3 answers

See items that match in two list

lest say I have two lists List1: "Tom", "Frank", "Lacey" List2: "Frank", "Tom" what would be the query needed to show that Tom and Fran are being repeated? The lists that I am trying to compare are very big and if I do something like: var q = from…
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
0
votes
0 answers

A list of unique sequences with EqualityComparer

I want to make a List> structure so that only distinct sequences of numbers can be added. For this I create an EqualityComparer this way: public class ListEqualityComparer : EqualityComparer> { Object…
jupiter_jazz
  • 333
  • 2
  • 8
0
votes
1 answer

Breakpoint in Equals implimentation on IEqualityComparer is never hit

I have a simple custom class Point: public class Point: IEqualityComparer { public double X; public double Y; public double Z; private double[] startPointCoords; public Point() { } public Point(double[]…
Eric Snyder
  • 1,816
  • 3
  • 22
  • 46
0
votes
1 answer

How to Implement IEquatable with Different Equality Checks

I have a MyCustomSet class with IEquatable implemented as shown below. This works fantastic when I want to check equality for all three sets (SetA*, SetB*, and SetC*). But requirements dictate that I also need the ability to check equality for only…
Ari Pub
  • 11
  • 4
0
votes
0 answers

True test of value type's default value?

In C#, how to properly check if a value type's value is truly default? In other words: whether all the elements are zero, as specified by the language? Using x == default requires for the struct to define an equality comparer, which could be…
relatively_random
  • 4,505
  • 1
  • 26
  • 48
0
votes
2 answers

IEnumerable.Contains is not using my own IEqualityComparer by default

I implemented IEqualityComparer and I was getting incorrect comparisons, not even watching the debugger passing through my Equals method when being used by IEnumerable.Contains method. public struct CustomMailAddress :…
Gonzo345
  • 1,133
  • 3
  • 20
  • 42