gethashcode

Equals, GetHashCode, EqualityComparers and fuzzy equality

与世无争的帅哥 提交于 2019-12-02 00:05:33
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.StartDate))) { equal = false; } return equal; } But it seems lots of places in the framework ignore Equals

Overriding GetHashCode()

為{幸葍}努か 提交于 2019-12-01 13:47:24
问题 In this article, Jon Skeet mentioned that he usually uses this kind of algorithm for overriding GetHashCode() . public override int GetHashCode() { unchecked // Overflow is fine, just wrap { int hash = 17; // Suitable nullity checks etc, of course :) hash = hash * 23 + Id.GetHashCode(); return hash; } } Now, I've tried using this, but Resharper tells me that the method GetHashCode() should be hashing using only read-only fields (it compiles fine, though). What would be a good practice,

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

旧时模样 提交于 2019-12-01 03:14:58
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? Yes! You should be overriding Equals and GetHashCode . But, you shouldn't be doing value equality ( Name == other.Name && Age == other

Reverse Engineering String.GetHashCode

≡放荡痞女 提交于 2019-11-30 23:11:43
String.GetHashCode's behavior is depend on the program architecture. So it will return one value in x86 and one value on x64. I have a test application which must run in x86 and it must predict the hash code output from an application which must run on x64. Below is the disassembly of the String.GetHashCode implementation from mscorwks. public override unsafe int GetHashCode() { fixed (char* text1 = ((char*) this)) { char* chPtr1 = text1; int num1 = 0x15051505; int num2 = num1; int* numPtr1 = (int*) chPtr1; for (int num3 = this.Length; num3 > 0; num3 -= 4) { num1 = (((num1 << 5) + num1) +

Do I need to override GetHashCode() on reference types?

不想你离开。 提交于 2019-11-30 17:14:06
I read most questions on StackOverflow with regards to GetHashCode . But I am still not sure whether I have to override GetHashCode on reference types. I picked up the following from someones answer in another question: Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object created is assigned a unique object key, stored as an integer,when it is created. These keys start at 1 and increment every time a new object of any type gets created. If this is still true in .NET Framework 3.5 (can someone please confirm?), then the only problem I

Hibernate n:m extractHashCode throws NullPointerException

ε祈祈猫儿з 提交于 2019-11-30 09:16:19
问题 I get the following exception while inserting an object with hibernate. Reading from the database works like a charm. I use MySQL 5.5 as database provider and hibernate 3.6.5 . I have the following database schema: cell(id,cellid,lac,mcc,mnc,insertTime) location(id,latitude,longitude,altitude,accuracy,heading,hdop,vdop,pdop,insertTime) cellatlocation(servingCell,neighbourCell,location,signalStrength,insertTime) where id in cell and location are primary keys and servingCell,neighbourCell and

How to pick prime numbers to calculate the hash code?

牧云@^-^@ 提交于 2019-11-30 07:36:56
This question follows on the answer given by Jon Skeet on the question: " What is the best algorithm for an overridden System.Object.GetHashCode? ". To calculate the hash code the following algorithm is used: public override int GetHashCode() { unchecked // Overflow is fine, just wrap { int hash = 17; // Suitable nullity checks etc, of course :) hash = hash * 23 + field1.GetHashCode(); hash = hash * 23 + field2.GetHashCode(); hash = hash * 23 + field3.GetHashCode(); return hash; } } I don't understand why the numbers 17 and 23 are chosen. Why don't we pick 3 and 5? That are prime numbers as

Object.GetHashCode

自古美人都是妖i 提交于 2019-11-30 06:18:59
My question may duplicate Default implementation for Object.GetHashCode() but I'm asking again because I didn't understand the accepted answer to that one. To begin with I have three questions about the accepted answer to the previous question , which quotes some documentation as follows: "However, because this index can be reused after the object is reclaimed during garbage collection, it is possible to obtain the same hash code for two different objects." Is this true? It seems to me that two objects won't have the same hash code, because an object's code isn't reused until the object is

Do I need to override GetHashCode() on reference types?

别来无恙 提交于 2019-11-30 00:42:40
问题 I read most questions on StackOverflow with regards to GetHashCode . But I am still not sure whether I have to override GetHashCode on reference types. I picked up the following from someones answer in another question: Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object created is assigned a unique object key, stored as an integer,when it is created. These keys start at 1 and increment every time a new object of any type gets created

GetHashCode for a Class with a List Object [duplicate]

南楼画角 提交于 2019-11-29 20:53:44
问题 This question already has answers here : Getting hash of a list of strings regardless of order (5 answers) Closed 5 years ago . I have such a class: public class Cycle { public List<int> Edges { get; private set; } public override bool Equals(object obj) { Cycle cycle = (Cycle)obj; var list1 = cycle.Edges; var list2 = Edges; var same = list1.Except(list2).Count() == 0 && list2.Except(list1).Count() == 0; return same; } public override int GetHashCode() { // return Edges.GetHashCode(); } } As