gethashcode

Using GetHashCode to test equality in Equals override

时光总嘲笑我的痴心妄想 提交于 2019-12-04 02:58:47
Is it ok to call GetHashCode as a method to test equality from inside the Equals override? For example, is this code acceptable? public class Class1 { public string A { get; set; } public string B { get; set; } public override bool Equals(object obj) { Class1 other = obj as Class1; return other != null && other.GetHashCode() == this.GetHashCode(); } public override int GetHashCode() { int result = 0; result = (result ^ 397) ^ (A == null ? 0 : A.GetHashCode()); result = (result ^ 397) ^ (B == null ? 0 : B.GetHashCode()); return result; } } The others are right; your equality operation is broken

How should we really be implenting Equals and GetHashCode for NHibernate entities

自古美人都是妖i 提交于 2019-12-04 00:27:07
问题 There are many questions and answers and articles to this question available but in my opinion there seems to be no real clear/correct answer For me Ayende has the best generic implementation so far that I've seen : http://ayende.com/blog/2500/generic-entity-equality ....But it is from 2007 .... Is this the 'best way' to implement these methods especially with regard to NHibernate 3.2 which contains some differences in proxy implementation to earlier versions? 回答1: Yes! You should be

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

十年热恋 提交于 2019-12-03 14:56:40
问题 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 GetHashCode() { if (ID != null) { return ID.GetHashCode(); } return 0; } Update 1: Updated 2nd option. Update 2: Below are the Equals implementations: public bool Equals(IContract other) { if (other == null) return false; if (this.ID.Equals(other.ID)) { return true; }

Equals method implementation helpers (C#)

一笑奈何 提交于 2019-12-03 14:14:13
Everytime I write some data class, I usually spend so much time writing the IEquatable implementation. The last class I wrote was something like: public class Polygon { public Point[] Vertices { get; set; } } Implementing IEquatable was exaustive. Surely C#3.0/LINQ helps a lot, but the vertices can be shifted and/or in the reverse order, and that adds a lot of complexity to the Equals method. After many unit tests, and corresponding implementation, I gave up, and changed my application to accept only triangles, which IEquatable implementation required only 11 unit tests to be fully covered.

GetHashCode() problem using xor

旧城冷巷雨未停 提交于 2019-12-03 12:35:50
My understanding is that you're typically supposed to use xor with GetHashCode() to produce an int to identify your data by its value (as opposed to by its reference). Here's a simple example: class Foo { int m_a; int m_b; public int A { get { return m_a; } set { m_a = value; } } public int B { get { return m_b; } set { m_b = value; } } public Foo(int a, int b) { m_a = a; m_b = b; } public override int GetHashCode() { return A ^ B; } public override bool Equals(object obj) { return this.GetHashCode() == obj.GetHashCode(); } } The idea being, I want to compare one instance of Foo to another

C#: How would you unit test GetHashCode?

断了今生、忘了曾经 提交于 2019-12-03 04:19:16
Testing the Equals method is pretty much straight forward (as far as I know). But how on earth do you test the GetHashCode method? Test that two distinct objects which are equal have the same hash code (for various values). Check that non-equal objects give different hash codes, varying one aspect/property at a time. While the hash codes don't have to be different, you'd be really unlucky to pick different values for properties which happen to give the same hash code unless you've got a bug. Gallio/MbUnit v3.2 comes with convenient contract verifiers which are able to test your implementation

Is .GetHashCode guaranteed to be the same across systems/platform versions? [duplicate]

你离开我真会死。 提交于 2019-12-02 13:28:54
Possible Duplicate: Can I depend on the values of GetHashCode() to be consistent? If I use the Object.GetHashCode() method across two systems/framework versions, am I guaranteed to get the same value for the same input? In other words, does its value make a good key for persistent data? Note: I don't care about collisions in this problem. As a bonus, am I guaranteed to get the same value in Mono vs. Microsoft .Net? No. Other questions? :-) The algorithms used aren't published nor they are in the Ecma standard. I'll quote from the MSDN String.GetHashCode (I think that this example is good

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

假装没事ソ 提交于 2019-12-02 07:27:05
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 Nothing Then Return MyBase.Equals(obj) If TypeOf obj Is Address Then Return Equals(DirectCast(obj, Address

Does this solve Nhibernate identity problem and GetHashCode issues?

Deadly 提交于 2019-12-02 02:32:11
The solution I propose involves quite a bit of code, but you can just copy it all and past it in a VS test solution assuming you have SqLite installed, and you should be able to run the tests yourself. As I have been struggling with the object identity versus object equality and database identity problem using Nhibernate, I have read various posts. However, I could not get a clear picture of how to properly set up object identity in conjunction with collections. Basically, the big problem, as I got, is that once an object is added to a collection it's identity (as derived by the GetHashCode)

Equals, GetHashCode, EqualityComparers and fuzzy equality

最后都变了- 提交于 2019-12-02 01:28:43
问题 For an object with properties A, B, C, D, StartDate and EndDate if I wanted to implement something where any two objects are equal if they have identical A, B and C and overlapping date range, how would that be done? I have tried creating an EqualityComparer like so public override bool Equals(RateItem x, RateItem y) { bool equal = true; if ((x.A != y.A || x.B != y.B || x.C != y.C || (x.StartDate < y.StartDate && x.EndDate <= y.StartDate) || (x.StartDate > y.StartDate && y.EndDate <= x