gethashcode

Generic IEqualityComparer<T> and GetHashCode

筅森魡賤 提交于 2019-11-29 14:04:58
问题 Being somewhat lazy about implementing lots of IEqualityComparers, and given that I couldn't easily edit class implementations of object being compared, I went with the following, meant to be used with Distinct() and Except() extension methods. : public class GenericEqualityComparer<T> : IEqualityComparer<T> { Func<T, T, bool> compareFunction; Func<T, int> hashFunction; public GenericEqualityComparer(Func<T, T, bool> compareFunction, Func<T, int> hashFunction) { this.compareFunction =

How to pick prime numbers to calculate the hash code?

我们两清 提交于 2019-11-29 09:38:16
问题 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

Object.GetHashCode

假装没事ソ 提交于 2019-11-29 07:00:26
问题 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

What to return when overriding Object.GetHashCode() in classes with no immutable fields?

无人久伴 提交于 2019-11-29 02:57:28
问题 Ok, before you get all mad because there are hundreds of similar sounding questions posted on the internet, I can assure you that I have just spent the last few hours reading all of them and have not found the answer to my question. Background: Basically, one of my large scale applications had been suffering from a situation where some Binding s on the ListBox.SelectedItem property would stop working or the program would crash after an edit had been made to the currently selected item. I

Why might a System.String object not cache its hash code?

余生长醉 提交于 2019-11-28 22:25:09
A glance at the source code for string.GetHashCode using Reflector reveals the following (for mscorlib.dll version 4.0): public override unsafe int GetHashCode() { fixed (char* str = ((char*) this)) { char* chPtr = str; int num = 0x15051505; int num2 = num; int* numPtr = (int*) chPtr; for (int i = this.Length; i > 0; i -= 4) { num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0]; if (i <= 2) { break; } num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1]; numPtr += 2; } return (num + (num2 * 0x5d588b65)); } } Now, I realize that the implementation of GetHashCode is not specified and is

Overriding GetHashCode [duplicate]

穿精又带淫゛_ 提交于 2019-11-28 21:07:43
This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 19 answers As you know, GetHashCode returns a semi-unique value that can be used to identify an object instance in a collection. As a good practice, it is recommended to override this method and implement your own. My question is - do you override this method when working on custom objects? If so, what algorithm do you use to generate the unique ID? I was thinking about generating a GUID and then getting integer data from that identificator. When you override GetHashCode() you also

GetHashCode Extension Method

落爺英雄遲暮 提交于 2019-11-28 17:06:45
After reading all the questions and answers on StackOverflow concerning overriding GetHashCode() I wrote the following extension method for easy and convenient overriding of GetHashCode() : public static class ObjectExtensions { private const int _seedPrimeNumber = 691; private const int _fieldPrimeNumber = 397; public static int GetHashCodeFromFields(this object obj, params object[] fields) { unchecked { //unchecked to prevent throwing overflow exception int hashCode = _seedPrimeNumber; for (int i = 0; i < fields.Length; i++) if (fields[i] != null) hashCode *= _fieldPrimeNumber + fields[i]

How are Equals and GetHashCode implemented on anonymous types?

天涯浪子 提交于 2019-11-28 12:36:52
The Help says this: Anonymous types are class types that derive directly from object, and that cannot be cast to any type except object. The compiler provides a name for each anonymous type, although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type. If two or more anonymous object initializers in an assembly specify a sequence of properties that are in the same order and that have the same names and types, the compiler treats the objects as instances of the same type. They share the same

Persistent hashcode for strings [duplicate]

穿精又带淫゛_ 提交于 2019-11-28 10:16:33
This question already has an answer here: How do I create a HashCode in .net (c#) for a string that is safe to store in a database? 3 answers I want to generate an integer hashcode for strings, that will stay constant forever; i.e. the same string should always result in the same hashcode. The hash does not have to be cryptographically secure, it will not be used for passwords or sensitive data. My first attempt was to use the .net framework string.GetHashCode() function. However upon reading the sources I found the following commment: // We want to ensure we can change our hash function daily

Why does C# not implement GetHashCode for Collections?

可紊 提交于 2019-11-28 09:47:34
I am porting something from Java to C#. In Java the hashcode of a ArrayList depends on the items in it. In C# I always get the same hashcode from a List ... Why is this? For some of my objects the hashcode needs to be different because the objects in their list property make the objects non-equal. I would expect that a hashcode is always unique for the object's state and only equals another hashcode when the object is equal. Am I wrong? In order to work correctly, hashcodes must be immutable – an object's hash code must never change. If an object's hashcode does change, any dictionaries