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
229
votes
4 answers

What's the difference between IEquatable and just overriding Object.Equals()?

I want my Food class to be able to test whenever it is equal to another instance of Food. I will later use it against a List, and I want to use its List.Contains() method. Should I implement IEquatable or just override Object.Equals()? From…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
179
votes
5 answers

What is the difference between IEqualityComparer and IEquatable?

I want to understand the scenarios where IEqualityComparer and IEquatable should be used. The MSDN documentation for both looks very similar.
Tilak
  • 30,108
  • 19
  • 83
  • 131
150
votes
11 answers

Distinct not working with LINQ to Objects

class Program { static void Main(string[] args) { List books = new List { new Book { Name="C# in Depth", Authors = new List { …
Tanmoy
  • 44,392
  • 16
  • 45
  • 55
113
votes
5 answers

What's the difference between IComparable & IEquatable interfaces?

Both the interfaces seem to compare objects for equality, so what are the major differences between them?
SoftwareGeek
  • 15,234
  • 19
  • 61
  • 78
57
votes
4 answers

When To Use IEquatable And Why

What does IEquatable buy you, exactly? The only reason I can see it being useful is when creating a generic type and forcing users to implement and write a good equals method. What am I missing?
Jack Kada
  • 24,474
  • 29
  • 82
  • 106
55
votes
5 answers

Is there a complete IEquatable implementation reference?

Many of my questions here on SO concerns IEquatable implementation. I found it being extremely difficult to implement correctly, because there are many hidden bugs in the naïve implementation, and the articles I found about it are quite incomplete.…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
53
votes
2 answers

Understanding IEquatable

When I implement objects that I want to compare using the IEquatable interface: Why do I have to override Equals(object) method if I already implemented Equals(T)? Can I use == and != operators once I implement IEquatable?
leora
  • 188,729
  • 360
  • 878
  • 1,366
38
votes
4 answers

Should IEquatable, IComparable be implemented on non-sealed classes?

Anyone have any opinions on whether or not IEquatable or IComparable should generally require that T is sealed (if it's a class)? This question occurred to me since I'm writing a set of base classes intended to aid in the implementation of…
Phil
  • 2,675
  • 1
  • 25
  • 27
27
votes
3 answers

What should GetHashCode return when object's identifier is null?

Which of the following is correct/better, considering that identity property could be null. public override int GetHashCode() { if (ID == null) { return base.GetHashCode(); } return ID.GetHashCode(); } OR public override int…
Dienekes
  • 1,548
  • 1
  • 16
  • 24
22
votes
2 answers

GetHashCode on null fields?

How do I deal with null fields in GetHashCode function? Module Module1 Sub Main() Dim c As New Contact Dim hash = c.GetHashCode End Sub Public Class Contact : Implements IEquatable(Of Contact) Public Name As String Public…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
20
votes
5 answers

Can I overload an == operator on an Interface?

I have an interface like this: public interface IFoo { int A {get;} int B {get;} } and I have multiple classes implementing IFoo. I want to check equality, not based on ReferenceEquality, but two IFoos should be considered equal, if both A and…
TDaver
  • 7,164
  • 5
  • 47
  • 94
20
votes
2 answers

Why does Equals(object) win over Equals(T) when using an inherited object in Hashset or other Collections?

I am aware of the fact that I always have to override Equals(object) and GetHashCode() when implementing IEquatable.Equals(T). However, I don't understand, why in some situations the Equals(object) wins over the generic Equals(T). For example why…
Marwie
  • 3,177
  • 3
  • 28
  • 49
19
votes
2 answers

Key comparisons for Linq GroupBy using Default EqualityComparer

I'm trying to do a Linq GroupBy on some objects using an explicit key type. I'm not passing an IEqualityComparer to the GroupBy, so according to the docs: The default equality comparer Default is used to compare keys. It explains the…
dan-gph
  • 16,301
  • 12
  • 61
  • 79
15
votes
4 answers

Should I use a concatenation of my string fields as a hash code?

I have an Address class in C# that looks like this: public class Address { public string StreetAddress { get; set; } public string RuralRoute { get; set; } public string City { get; set; } public string Province { get;…
cdmckay
  • 31,832
  • 25
  • 83
  • 114
10
votes
2 answers

How should I go about implementing Object.GetHashCode() for complex equality?

Basically, I have the following so far: class Foo { public override bool Equals(object obj) { Foo d = obj as Foo ; if (d == null) return false; return this.Equals(d); } #region IEquatable
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
1
2 3
10 11