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
1
vote
3 answers

If I implement IEquatable, will I lose the option to compare by reference?

I would like to compare an object with antoher to know if they are equal or not. So it seems the way to do that is implementing the IEquatable interface in my class. But I am not sure about how this affect to the behaviour of my class. Now, in my…
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
1
vote
1 answer

Overriding IEquatable when T is an interface and hashcodes are different between derived types

I have A and B classes both implementing interface I. public interface I { int SomeInt { get; } bool SomeBool { get; } float SomeFloat { get; } } public class A : I { public int SomeInt { get; } public bool SomeBool { get; } …
asaf92
  • 1,557
  • 1
  • 19
  • 30
1
vote
2 answers

Net Core: Code Generation for IEquatable/Equals for Comparing Two Class Models

We scaffolded database from Entity Framework to create class models. We have over 1000 classes. Now we're implementing unit tests, to compare classes inserting an Actual class with Expected class. The following website recommends method below to…
user11860043
1
vote
1 answer

Set's contains method returns different value at different time

I was thinking about how Swift ensures uniqueness for Set because I have turned one of my obj from Equatable to Hashable for free and so I came up with this simple Playground struct SimpleStruct: Hashable { let string: String let number:…
ndPPPhz
  • 315
  • 1
  • 15
1
vote
2 answers

overloaded == operator throwing NullReferenceException with non-null operands

I'm trying to implement the IEquatable interface and == operator. The implementation below triggers a NullReferenceException when I try to use the == operator despite the fact that both operands are non-null. I've added comments to the minimum…
Verwirrt
  • 403
  • 2
  • 13
1
vote
3 answers

Implementing Equals and GetHashCode - an easier way

I have a tree of objects (DTOs), where one object references other objects and so on: class Person { public int Id { get; } public Address Address { get; } // Several other properties } public Address { public int Id { get; } …
Don Box
  • 3,166
  • 3
  • 26
  • 55
1
vote
2 answers

Dynamic equality checking of multiple properties of a type's objects

I have a type like: class Order { public List AllItems { get; set; } public string Name { get; set; } public double TotalPurchases { get; set; } public long Amount { get; set; } public int Code { get; set; } } I've implemented the…
Dienekes
  • 1,548
  • 1
  • 16
  • 24
1
vote
0 answers

Compare two object that themself have ienumerables

I have an class like this: public class Inner { public int foo; public int bar; } and an outer class: public class Outer { public string Name; public IEnumerable foobars; } When I compare two objects, I do this: // I have to…
galmok
  • 869
  • 10
  • 21
1
vote
1 answer

Comparing Collection of Objects with ContainsExactly

I am trying to compare two collections of objects that have the exact same objects in their respective collections. I wrote an extension method for ContainsExactly to do so. However, I am running into an issue where it is saying the collections are…
Dave
  • 45
  • 5
1
vote
1 answer

DDD Entity - are two entities with default identity considered equal

I want to apply/practice DDD with my new project hence I am creating those typical DDD base classes, i.e., Entity, ValueObject, AggregateRoot and so on. Question: When you have the Entity base object implement IEquatable, should two entities with…
David Liang
  • 20,385
  • 6
  • 44
  • 70
1
vote
2 answers

LINQ.Distinct on IEquatable object doesn't work

I've an object that inherites from a base class that inherits from IEquatable<>. So far so good and it works for other objects that inherit the same base class. But I have on class "RoomType" where there seems to be a problem when I use the…
BvdVen
  • 2,921
  • 23
  • 33
1
vote
5 answers

C# generic list of my class contains method not finding my instance

We have several classes in place for our CMS and I'm trying to get equality to work so I can check to see if a generic List contains an item. We have some layers of inheritance which I'll show you below. Below that, I'll show you some sample code…
Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
1
vote
0 answers

changing ReSharper Template for IEquatable and IEqualityComparer

Is there a way to change the implementation details, when you add a IEquatable or IEqualityComparer with ReSharper to your own class? I am using R# Ultimate 2016.3 I want to have some brackets around single if statement, because of StyleCop…
Dom84
  • 852
  • 7
  • 20
1
vote
0 answers

Comparing independently by Id value and by property values

I am working with an abstract class RepositoryItemBase as follows: public abstract class RepositoryItemBase : ICloneable, IEquatable { protected internal Guid Id => Guid.NewGuid(); protected RepositoryItemBase() { } public object…
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
1
vote
1 answer

Equals override vs. IEquatable<>

For the life of me, I can't get my WPF binding to work correctly for a RibbonComboBox's SelectedItem property. Then I started reading about how .NET compares items. My understanding is that, in some cases, it compares the actual pointer. In which…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466