gethashcode

How is GetHashCode() of C# string implemented?

风流意气都作罢 提交于 2019-11-26 18:51:30
I'm just curious because I guess it will have impact on performance. Does it consider the full string? If yes, it will be slow on long string. If it only consider part of the string, it will have bad performance (e.g. if it only consider the beginning of the string, it will have bad performance if a HashSet contains mostly strings with the same. Be sure to obtain the Reference Source source code when you have questions like this. There's a lot more to it than what you can see from a decompiler. Pick the one that matches your preferred .NET target, the method has changed a great deal between

How do I create a HashCode in .net (c#) for a string that is safe to store in a database?

ぐ巨炮叔叔 提交于 2019-11-26 18:48:26
To quote from Guidelines and rules for GetHashCode by Eric Lippert: Rule: Consumers of GetHashCode cannot rely upon it being stable over time or across appdomains Suppose you have a Customer object that has a bunch of fields like Name, Address, and so on. If you make two such objects with exactly the same data in two different processes, they do not have to return the same hash code. If you make such an object on Tuesday in one process, shut it down, and run the program again on Wednesday, the hash codes can be different. This has bitten people in the past. The documentation for System.String

Is there a complete IEquatable implementation reference?

女生的网名这么多〃 提交于 2019-11-26 17:57:50
问题 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

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

徘徊边缘 提交于 2019-11-26 16:52:44
问题 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

Why is ValueType.GetHashCode() implemented like it is?

我是研究僧i 提交于 2019-11-26 16:17:31
From ValueType.cs **Action: Our algorithm for returning the hashcode is a little bit complex. We look ** for the first non-static field and get it's hashcode. If the type has no ** non-static fields, we return the hashcode of the type. We can't take the ** hashcode of a static member because if that member is of the same type as ** the original type, we'll end up in an infinite loop. I got bitten by this today when I was using a KeyValuePair as a key in a Dictionary (it stored xml attribute name (enum) and it's value (string)), and expected for it to have it's hashcode computed based on all

Overriding GetHashCode in VB without checked/unchecked keyword support?

前提是你 提交于 2019-11-26 15:59:37
问题 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:

GetHashCode() gives different results on different servers?

ⅰ亾dé卋堺 提交于 2019-11-26 14:22:46
问题 I declared a C# line of code like so int hashcode = "apple".GetHashCode(); On my computer, a computer at work, and a friend's computer, the result was 1657858284. On a development server, the result was 1548091822. Is there a way for me to tell the project to always make GetHashCode() yield 1657858284, regardless of which server it is on? more notes At first, i noticed there was a difference in versions...the 1657858284 results came from .NET 3.5 and .NET 4.0. The 1548091822 came from .NET 2

What&#39;s the role of GetHashCode in the IEqualityComparer<T> in .NET?

ぐ巨炮叔叔 提交于 2019-11-26 12:36:17
问题 I\'m trying to understand the role of the GetHashCode method of the interface IEqualityComparer. The following example is taken from MSDN: using System; using System.Collections.Generic; class Example { static void Main() { try { BoxEqualityComparer boxEqC = new BoxEqualityComparer(); Dictionary<Box, String> boxes = new Dictionary<Box, string>(boxEqC); Box redBox = new Box(4, 3, 4); Box blueBox = new Box(4, 3, 4); boxes.Add(redBox, \"red\"); boxes.Add(blueBox, \"blue\"); Console.WriteLine

When Should a .NET Class Override Equals()? When Should it Not?

心已入冬 提交于 2019-11-26 11:22:10
问题 The VS2005 documentation Guidelines for Overloading Equals() and Operator == (C# Programming Guide) states in part Overriding operator == in non-immutable types is not recommended. The newer .NET Framework 4 documentation Guidelines for Implementing Equals and the Equality Operator (==) omits that statement, although one post in Community Content repeats the assertion and references the older documentation. It seems that it is reasonable to override Equals() at least for some trivial mutable

Overriding GetHashCode for mutable objects?

感情迁移 提交于 2019-11-26 10:26:00
问题 I\'ve read about 10 different questions on when and how to override GetHashCode but there\'s still something I don\'t quite get. Most implementations of GetHashCode are based on the hash codes of the fields of the object, but it\'s been cited that the value of GetHashCode should never change over the lifetime of the object. How does that work if the fields that it\'s based on are mutable? Also what if I do want dictionary lookups etc to be based on reference equality not my overridden Equals