Questions tagged [iequatable]

Interface, which defines a generalized method that a value type or class implements to create a type-specific method for determining equality of instances.

165 questions
9
votes
9 answers

Implement GetHashCode on a class that has wildcard Equatability

Suppose I want to be able to compare 2 lists of ints and treat one particular value as a wild card. e.g. If -1 is a wild card, then {1,2,3,4} == {1,2,-1,4} //returns true And I'm writing a class to wrap all this logic, so it implements IEquatable…
Brondahl
  • 7,402
  • 5
  • 45
  • 74
9
votes
5 answers

Howto override List Contains

I want to compare a property instead of the entire object using a List[MyObject]. I therefore use IEquatable[MyObject] but the compiler still wants MyObject instead of the string property. Why? Here is what I got: public class AnyClass { public…
UNeverNo
  • 549
  • 3
  • 8
  • 29
8
votes
2 answers

Equals method implementation helpers (C#)

Everytime I write some data class, I usually spend so much time writing the IEquatable implementation. The last class I wrote was something like: public class Polygon { public Point[] Vertices { get; set; } } Implementing IEquatable was…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
8
votes
3 answers

Why is Array.IndexOf not checking for IEquatable like List does?

Consider this code: public static void Main() { var item = new Item { Id = 1 }; IList list = new List { item }; IList array = new[] { item }; var newItem = new Item { Id = 1 }; var lIndex = list.IndexOf(newItem); var…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
8
votes
1 answer

Class implementation of IEquatable for use as a key in a dictionary

I've got a class which consists of two strings and an enum. I'm trying to use instances of this class as keys in a dictionary. Unfortunately I don't seem to be implementing IEquatable properly. Here's how I've done it: public enum CoinSide { …
user2823789
  • 167
  • 1
  • 3
  • 10
8
votes
3 answers

Is it important to override Equals if I'm implementing IEquatable?

I know the importance of overriding GetHashCode when implementing custom equality checks - for which I have implemented IEquality interface, and also the difference between generic and non-generic Equals as discussed here. Now is there a point to…
nawfal
  • 70,104
  • 56
  • 326
  • 368
7
votes
2 answers

Generic class that conforms to Comparable in Swift

I'm attempting to create a simple generic node class that conforms to the Comparable protocol so that I can easily compare nodes without accessing their key. When I attempt to write the < and == functions, however, the compiler doesn't seem to like…
Daniel Rushton
  • 480
  • 5
  • 13
6
votes
3 answers

Implement IEquatable for POCO

I noticed that EF's DbSet.Add() is quite slow. A little googling turned up a SO answer that promises up to 180x performance gains: https://stackoverflow.com/a/7052504/141172 However, I do not understand exactly how to implement IEquatable as…
Eric J.
  • 147,927
  • 63
  • 340
  • 553
6
votes
8 answers

IEquatable Interface what to do when checking for null

I have implemented the IEquatable interface in a class with the following code. public bool Equals(ClauseBE other) { if (this._id == other._id) { return true; } return…
Matthew Vines
  • 27,253
  • 7
  • 76
  • 97
6
votes
3 answers

Should GetHashCode be implemented for IEquatable on mutable types?

I'm implementing IEquatable, and I am having difficulty finding consensus on the GetHashCode override on a mutable class. The following resources all provide an implementation where GetHashCode would return different values during the object's…
Neo
  • 4,145
  • 6
  • 53
  • 76
6
votes
2 answers

Value vs. Reference equality in generic List.Contains()

Attempt #3 to simplify this question: A generic List can contain any type - value or reference. When checking to see if a list contains an object, .Contains() uses the default EqualityComparer for type T, and calls .Equals() (is my…
James King
  • 6,233
  • 5
  • 42
  • 63
5
votes
2 answers

How to compare two IEnumerable in C# if I don't know the actual object type?

I'm struggling with implementing the IEquatable<> interface for a class. The class has a Parameter property that uses a generic type. Basically the class definition is like this: public class MyClass : IEquatable> { public T…
Baldewin
  • 1,613
  • 2
  • 16
  • 23
5
votes
1 answer

Custom object using Except failing to use IEqualityComparer

here is the object code: public class DlpItem : IEqualityComparer { public string Text { get; set; } public int Id { get; set; } public DlpItem(int pId) { Text = string.Empty; Id = pId; } public…
Matthew Cox
  • 13,566
  • 9
  • 54
  • 72
5
votes
2 answers

Testing for value equality between two interface instances in c#?

So I have an interface, lets call it IInterface. public interface IInterface : IEquatable { string Name { get; set; } int Number { get; } Task Update(); } Then I try and implement the interface in Implementation. …
bodangly
  • 2,473
  • 17
  • 28
5
votes
1 answer

Compiler picking wrong overload calling IEquatable.Equals

In a performance sensitive program, I am attempting to explicitly call IEquatable.Equals() and not Object.Equals (to avoid boxing in my case). Despite my best efforts, the compiler is always choosing Object.Equals() instead - which I don't…
user5876832
1
2
3
10 11