gethashcode

Using GetHashCode for getting Enum int value

好久不见. 提交于 2019-11-28 07:24:21
I have an enum public enum INFLOW_SEARCH_ON { ON_ENTITY_HANDLE = 0, ON_LABEL = 1, ON_NODE_HANDLE = 2 } // enum INFLOW_SEARCH_ON I have to use this enum for searching in a grid column. To get the column index I am using MyEnumVariable.GetHashCode() Which works ok, or should I use (short)MyEnumVariable I am a bit confused over using GetHashCode() . Is there any problem using that? Using GetHashCode() is incorrect. You should cast to int . Using it the way you do is asking for raptors(or Raymond) to come and eat you. That GetHashCode() happens to return the integer value of the enum is an

string.GetHashCode() returns different values in debug vs release, how do I avoid this?

孤街浪徒 提交于 2019-11-28 03:52:34
问题 To my surprise the folowing method produces a different result in debug vs release: int result = "test".GetHashCode(); Is there any way to avoid this? I need a reliable way to hash a string and I need the value to be consistent in debug and release mode. I would like to avoid writing my own hashing function if possible. Why does this happen? FYI, reflector gives me: [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail), SecuritySafeCritical] public override unsafe int GetHashCode

Equals vs GetHashCode when comparing objects

。_饼干妹妹 提交于 2019-11-27 22:22:11
Should we override both Equals and GetHashCode properties when implementing a custom class instances comparison? In the following code I have a collection of classes. The class A is compared by the ID , the class B - by Code . using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { List<I> bars = new List<I>(); bars.Add(new A() { Id = 1, Code = "one A" }); bars.Add(new B() { Id = 1, Code = "one B" }); bars.Add(new A() { Id = 1, Code = "one A+" }); bars.Add(new B() { Id = 1,

Is it possible to combine hash codes for private members to generate a new hash code?

荒凉一梦 提交于 2019-11-27 19:49:33
I have an object for which I want to generate a unique hash (override GetHashCode()) but I want to avoid overflows or something unpredictable. The code should be the result of combining the hash codes of a small collection of strings. The hash codes will be part of generating a cache key, so ideally they should be unique however the number of possible values that are being hashed is small so I THINK probability is in my favour here. Would something like this be sufficient AND is there a better way of doing this? int hash = 0; foreach(string item in collection){ hash += (item.GetHashCode() /

Overriding GetHashCode in VB without checked/unchecked keyword support?

不羁岁月 提交于 2019-11-27 19:23:58
So I'm trying to figure out how to correctly override GetHashCode() in VB for a large number of custom objects. A bit of searching leads me to this wonderful answer . Except there's one problem: VB lacks both the checked and unchecked keyword in .NET 4.0. As far as I can tell, anyways. So using Jon Skeet's implementation, I tried creating such an override on a rather simple class that has three main members: Name As String , Value As Int32 , and [Type] As System.Type . Thus I come up with: Public Overrides Function GetHashCode() As Int32 Dim hash As Int32 = 17 hash = hash * 23 + _Name

new KeyValuePair<UInt32, UInt32>(i, j).GetHashCode(); High Rate of Duplicates

半腔热情 提交于 2019-11-27 14:43:56
In search of a fast composite key for Dictionary I came upon anomaly I cannot understand nor justify. In limited testing Dictionary<KeyValuePair<UInt32, UInt32>, string> is significantly slower (200:1) than Dictionary<KeyValuePair<UInt16, UInt16>, string> Test on two loops from 0 to 1000 Populate and then ContainsKey Poplulate ContainsKey UInt32 92085 86578 UInt16 2201 431 The problem is that new KeyValuePair<UInt32, UInt32>(i, j).GetHashCode(); yields MANY duplicates. In looping i and j 1024 only 1024 unique hash values are created. Based on avalanche comment from CasperOne tried i*31 and j

Overriding GetHashCode [duplicate]

放肆的年华 提交于 2019-11-27 13:29:31
问题 This question already has answers here : What is the best algorithm for overriding GetHashCode? (19 answers) Closed 2 years ago . 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

Is there a complete IEquatable implementation reference?

冷暖自知 提交于 2019-11-27 10:36:49
Many of my questions here on SO concerns IEquatable implementation. I found it being extremely difficult to implement correctly, because there are many hidden bugs in the naïve implementation, and the articles I found about it are quite incomplete. I want to find or write a definitive reference which must include: How to implement IEquatable correctly How to override Equals correctly How to override GetHashCode correctly How to implement the ToString method correctly How to implement the operator == correctly How to implement the operator != correctly Such a complete reference already exists?

GetHashCode Extension Method

≡放荡痞女 提交于 2019-11-27 10:11:22
问题 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

Why use GetHashCode() over Equals()?

a 夏天 提交于 2019-11-27 07:40:03
问题 HashSet<T>.Add first compares the results of GetHashCode . If those are equal, it calls Equals . Now, my understanding is in order to implement GetHashCode , something must be done with the fields of an object. A simple example implementation can be found at What is the best algorithm for an overridden System.Object.GetHashCode?. In my test comparing both on 1.000.000 pairs of objects filled with random data, performance is more or less equal between the two. GetHashCode is implemented as in