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
5
votes
1 answer

Comparing two collections with IEquatable while using only LINQ

This question is for educational purposes only and I can solve it easily by using for loop that returns false on first mismatch. I am implementing IEquatable on CustomerFeedbackViewModel which has a collection of…
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
5
votes
2 answers

Should I be using IEquatable to ease testing of factories?

I often work with classes that represent entities produced from a factory. To enable easy testing of my factories easily I usually implement IEquatable, whilst also overriding GetHashCode and Equals (as suggested by the MSDN). For example; take…
James Wood
  • 17,286
  • 4
  • 46
  • 89
5
votes
1 answer

My IEquatable is still using Object.GetHashcode for Dictionary[]

I have something like the following to be a key for a generic dictionary. class IMyClass : IEquatable where T : struct { //etc } class MyClass : IMyClass where T : struct { public bool Equals(IRatingKey other) { …
Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
5
votes
2 answers

How to use Object.GetHashCode() on a type that overrides GetHashCode()

I have a class A that implements IEquatable<>, using its fields (say, A.b and A.c) for implementing/overriding Equals() and overriding GetHashCode(), and everything works fine, 99% of the time. Class A is part of a hierarchy (class B, C) that all…
Jimmy
  • 5,131
  • 9
  • 55
  • 81
5
votes
2 answers

When to specify constraint `T : IEquatable` even though it is not strictly required?

In short, I am looking for guidance on which of the following two methods should be preferred (and why): static IEnumerable DistinctA(this IEnumerable xs) { return new HashSet(xs); } static IEnumerable DistinctB(this…
4
votes
2 answers

Difference between Object.Equals(objA, objB), objA.Equals(objB) and objA == objB for CLR types?

I am wondering if the CLR types would return different results from the following: Object.Equals(objA, objB) objA.Equals(objB) (objA == objB) I do realize that outside of the CLR someone could easily implement the IEqualtable Equals and overload…
michael
  • 14,844
  • 28
  • 89
  • 177
4
votes
3 answers

IEquatable doesn't work with static Equals method

I implemented a class called NonEmptyString which doesn't allow creation when it's not empty. I made this class implement IEquatable and IEquatable. I have overrides for Equals(object obj), Equals(NonEmptyString other),…
4
votes
5 answers

Value Equality with Bidirectional Association in C#

Background I have two objects which have bidirectional association between them in a C# project I am working on. I need to be able to check for value equality (vs reference equality) for a number of reasons (e.g to use them in collections) and so I…
Zach Burlingame
  • 13,476
  • 14
  • 56
  • 65
4
votes
0 answers

C# iequatable == example, why use (object)myInstace to test null

So in one of the example given by C#. https://learn.microsoft.com/en-us/dotnet/api/system.iequatable-1.equals?view=netframework-4.7.1 They add a ==-operator in a class, but the part I don't understand is why they use (object)target == null to check…
libra
  • 673
  • 1
  • 10
  • 30
4
votes
1 answer

Linq .Except function "At least one object must implement IComparable."

Basically i have a container which implements IEquatable (sample shown below) public class ContainerClass : IEquatable { public IEnumerable CustomClass { get; set; } public override bool Equals(object…
Manatherin
  • 4,169
  • 5
  • 36
  • 52
4
votes
1 answer

Handling collections in GetHashCode implementation

I'm working on implementing GetHashCode() based on the HashCode struct in this answer here. Since my Equals method will consider collections using Enumerable.SequenceEqual(), I need to include the collections in my GetHashCode() implementation. As a…
brdmllr
  • 43
  • 1
  • 6
4
votes
2 answers

Implementing IEquatable in Base Class

I am using ASP.NET MVC with EntityFramework code first. I am attempting to create a base class for all objects that will be saved to my database (here called the Entity class). Is there a good way of implementing IEquatable in the base class? In…
William
  • 3,335
  • 9
  • 42
  • 74
4
votes
2 answers

What are the drawbacks of using a Guid().GetHashCode() when overriding GetHashCode()

I found an implementation of GetHashCode() that looks like this Guid _hashCode = Guid.NewGuid(); public override int GetHashCode() { return _hashCode.GetHashCode(); } Even thought the Equals looks correct, is it correct to…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
4
votes
2 answers

How to test for NaN with generics (or why NaN.Equals(NaN) == true)?

I need to find min and max values in an array (not taking into for possible NaN values in this array). This would be easy only working with double, but these FindMin and FindMax functions have to work with generics types. I have tried to test for…
CitizenInsane
  • 4,755
  • 1
  • 25
  • 56
3
votes
2 answers

Have implemented IEquatable correctly? Should I always override GetHashCode?

I saw the question posed here: Have I implemented Equals()/GetHashCode() correctly? but my c# is not as strong, and I am unfimiliar with IEquatable enough that I would like to see this in VB.NET if possible please. My example code (The class will…
Omar Mir
  • 1,500
  • 1
  • 19
  • 39
1 2
3
10 11