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
3
votes
2 answers

EqualityComparerer.Default.Equals() vs object.Equals() and polymorphism

Once again discussing equality I stumbled on EqualityComparer.Default.Equals(). I prefer to call this method for reference types rather than object.Equals(). Now I think I was dreadfully wrong. object.Equals() uses overridable instance Equals()…
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
3
votes
1 answer

GetHashCode is implemented but Dictionary is unable to find the key?

In my class, I have implemented Equals and GetHashCode. yet when I use it as key for a dictionary in my C# code, I get the error : "Key not found exception" Thanks, public class Time: IEquatable
Saeid Farivar
  • 1,667
  • 24
  • 43
3
votes
8 answers

How to treat nulls in equality comparisons?

When I have to implement equality comparers for public class SampleClass { public int Prop { get; set; } } Should I make null == new SampleClass() and new SampleClass() == null and new SampleClass().Equals(null) false? And what about new…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
3
votes
2 answers

Should GetHashCode() return value be based on original object's state or the modified object's state?

I've asked this question recently a few different ways, but don't get an answer that tells me how a Dictionary of needs to be handled when I hold a reference to something that changes T.GetHashCode(). For the purpose of this question "state"…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
2
votes
3 answers

For reference types how does using IEquatable reduce the use of casting?

I've read in several articles that for reference types using IEquatable reduces the use of casting Can someone kindly provide a convincing example.
Clarry
2
votes
3 answers

Find an item inside a List by providing a sample object instance

Why is there a List.Contains(T) method but no List.Find(T) method? Only the Finds that support predicates are supported. If we have an existing instance of T populated with a property value for its ID (but missing other properties) why can't…
mare
  • 13,033
  • 24
  • 102
  • 191
2
votes
0 answers

IEquatable with GetHashCode?

I have created a Graph Class including HashSet as Nodes and HashSet> as Edges. public interface IEdge { T A_Node { get; } T Another_Node { get; } } public interface IGraph { ISet Nodes { get; } ISet>…
Parsa
  • 7,995
  • 2
  • 27
  • 37
2
votes
4 answers

collections.Contains(T) method

I am using a System.Collections.Generic, which contains instances of a class I wrote. I have read that the collections .Contains method uses object.Equals(), or an implementation of the Equals() method from the IEquatable interface. I have…
peace_within_reach
  • 237
  • 2
  • 4
  • 14
2
votes
3 answers

When I compare an Object (type) does it uses the IEquatable of a specific class?

My method receives two parameters, both of Object type. They have the same type, that implements IEquatable. My question is: when I do: param1 == param2 does the framework compare using the IEquatable operator override of specific class or does it…
Thiago Bernabé
  • 109
  • 1
  • 8
2
votes
2 answers

Linq Distinct with a single comparison class (and interface)

I have several classes in my application, all of which have a Name property that I want to use as my basis for comparison (Distinct(), etc.). Since I am always going to be comparing on Name, I decided to extract an interface, ISomeComparedStuff,…
Brian David Berman
  • 7,514
  • 26
  • 77
  • 144
2
votes
5 answers

How do I get Distinct() to work with a collection of custom objects

I have followed the suggestions from this post to try and get Distinct() working in my code but I am still having issues. Here are the two objects I am working with: public class InvoiceItem : IEqualityComparer { public…
swolff1978
  • 1,845
  • 7
  • 28
  • 44
2
votes
1 answer

How to override in IEquatable so as to use Contain and Exists methods in a List with different types

using System; using System.Collections.Generic; //This is my class for obtaining certain values, putting them in a List //and then use the List to do certain operations public class SRvaluesChecker { double currentPriceValue; int…
2
votes
2 answers

swift - Comparing structs that conform to a protocol

I have the following structs that represent a point or a line: public struct Point{ let x : Double let y : Double init (x : Double, y : Double) { self.x = x self.y = y } } extension Point : Equatable{} …
Franklin
  • 881
  • 1
  • 8
  • 28
2
votes
2 answers

Custom Contains for List c#

I'm trying to use List.Contains in a List My objects to compare come from a Service Reference in C# and their Equals method doesn't suit my needs. I've been looking into IEquatables or on how to override my Equals method in an objet I'm "given" but…
jacandau
  • 197
  • 1
  • 12
2
votes
5 answers

C# dictionary uniqueness for sibling classes using IEquatable

I would like to store insances of two classes in a dictionary structure and use IEquatable to determine uniqueness of these instances. Both of these classes share an (abstract) base class. Consider the following classes: abstract class Foo { …
anthony
  • 40,424
  • 5
  • 55
  • 128