iequatable

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

不羁岁月 提交于 2021-02-19 07:34:08
问题 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; } public float SomeFloat { get; } private readonly string _someARelatedStuff; // Rest of class... } public class B : I { public int SomeInt { get; } public bool SomeBool { get; } public float SomeFloat { get; } private string readonly _someBRelatedStuff; private double

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

﹥>﹥吖頭↗ 提交于 2021-02-19 07:33:44
问题 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; } public float SomeFloat { get; } private readonly string _someARelatedStuff; // Rest of class... } public class B : I { public int SomeInt { get; } public bool SomeBool { get; } public float SomeFloat { get; } private string readonly _someBRelatedStuff; private double

Using LINQ GroupBy to group by reference objects instead of value objects

匆匆过客 提交于 2020-08-26 09:56:13
问题 I want to GroupBy multiple objects in a list of records and not simply multiple values. I am having trouble getting grouping to work with reference type objects. I have a collection of objects that contain Room, Type, and DateTime. Room, Type, and DateTime all have properties associated with them. I've already added the IEquateable interface to the room and the type thinking that would be enough to with with group by. var groups = collection .Where(g => g.Stage == InventoryStage.StageA)

Custom Contains for List<ReferenceObject> c#

ぐ巨炮叔叔 提交于 2020-01-04 16:57:25
问题 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 I can't seem to find a solution for this. Does some one know an efficient way to do this? public void FilterNonExisting(List<ActivitiesActivity> commitActivitiesList) { // ActivitiesActivity is the object I'm given through a reference List<int>

How can two generic linked list in swift can be compared?

前提是你 提交于 2020-01-04 05:35:35
问题 I have a generic linked list and I can check if two linked list are equal if each of the node value are same and are in order. I have a function which divides linked list in two part and later I want to check two list has same value in it's node. func divideList(atIndex index:Int) -> (first: LLGeneric<T>?,second: LLGeneric<T>?) I looking it for my use case where I can check palindrome in linked list after dividing and then comparing ( after reversing one list). Note: my linked list node is

Should I be using IEquatable to ease testing of factories?

℡╲_俬逩灬. 提交于 2020-01-03 11:57:19
问题 I often work with classes that represent entities produced from a factory. To enable easy testing of my factories easily I usually implement IEquatable<T> , whilst also overriding GetHashCode and Equals (as suggested by the MSDN). For example; take the following entity class which is simplified for example purposes. Typically my classes have a bunch more properties. Occasionally there is a also collection, which in the Equals method I check using SequenceEqual . public class Product :

What's the difference between IComparable & IEquatable interfaces?

痴心易碎 提交于 2019-12-28 08:02:10
问题 both the interfaces seem to compare objects for equality, so what's the major differences between them? 回答1: IEquatable tests whether two objects are equal. IComparable imposes a total ordering on the objects being compared. For example, IEquatable would tell you that 5 is not equal to 7. IComparable would tell you that 5 comes before 7. 回答2: IEquatable<T> for equality. IComparable<T> for ordering. 回答3: In addition to Greg D's answer: You might implement IComparable without implementing

Implementing IEquatable<T> in a mutable type

被刻印的时光 ゝ 提交于 2019-12-23 16:10:44
问题 I have a class that represents an external physical measuring device. The simplified version looks like this: public class Device { public string Tag { get; set; } public int Address { get; set; } } Tag is a user-defined value for identifying the device. Address is the value used by an adapter to communicate with the device. If two instances of Device have the same Address , then the same external measuring device will be used. I'd like to mimic that behavior in code (for using methods like

Implement IEquatable for POCO

我的梦境 提交于 2019-12-21 12:55:47
问题 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<T> as suggested in the answer. According to MSDN, if I implement IEquatable<T> , I should also override Equals() and GetHashCode() . As with many POCO's, my objects are mutable . Before being committed to the database ( SaveChanges() ), new objects have an

How to implement IEquatable<T> when mutable fields are part of the equality - Problem with GetHashCode

…衆ロ難τιáo~ 提交于 2019-12-20 05:28:04
问题 I am using Entity Framework in my application. I implemented with the partial class of an entity the IEquatable<T> interface: Partial Class Address : Implements IEquatable(Of Address) 'Other part generated Public Overloads Function Equals(ByVal other As Address) As Boolean _ Implements System.IEquatable(Of Address).Equals If ReferenceEquals(Me, other) Then Return True Return AddressId = other.AddressId End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If obj Is