gethashcode

Seeding a pseudo-random number generator in C#

流过昼夜 提交于 2019-11-27 06:38:08
问题 I need a seed for an instance of C#'s Random class, and I read that most people use the current time's ticks counter for this. But that is a 64-bit value and the seed needs to be a 32-bit value. Now I thought that the GetHashCode() method, which returns an int , should provide a reasonably distributed value for its object and this may be used to avoid using only the lower 32-bits of the tick count. However, I couldn't find anything about the GetHashCode() of the Int64 datatype. So, I know

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

独自空忆成欢 提交于 2019-11-27 05:04:57
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 classes, such as public class ImaginaryNumber { public double RealPart { get; set; } public double

Why does C# not implement GetHashCode for Collections?

五迷三道 提交于 2019-11-27 03:11:54
问题 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? 回答1: In order to work correctly, hashcodes must be

Overriding GetHashCode for mutable objects?

删除回忆录丶 提交于 2019-11-27 03:10:47
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 ? I'm primarily overriding Equals for the ease of unit testing my serialization code which I assume

Persistent hashcode for strings [duplicate]

落爺英雄遲暮 提交于 2019-11-27 02:57:29
问题 This question already has answers here : How do I create a HashCode in .net (c#) for a string that is safe to store in a database? (3 answers) Closed 3 years ago . 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

What's the role of GetHashCode in the IEqualityComparer<T> in .NET?

岁酱吖の 提交于 2019-11-27 02:54:53
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(redBox.GetHashCode()); Console.WriteLine(blueBox.GetHashCode()); } catch (ArgumentException argEx) { Console

Using GetHashCode for getting Enum int value

血红的双手。 提交于 2019-11-27 01:48:28
问题 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? 回答1: Using GetHashCode() is incorrect. You should cast to int . Using it the way you do is asking for raptors(or

.NET unique object identifier

吃可爱长大的小学妹 提交于 2019-11-26 21:33:54
Is there a way of getting a unique identifier of an instance? GetHashCode() is the same for the two references pointing to the same instance. However, two different instances can (quite easily) get the same hash code: Hashtable hashCodesSeen = new Hashtable(); LinkedList<object> l = new LinkedList<object>(); int n = 0; while (true) { object o = new object(); // Remember objects so that they don't get collected. // This does not make any difference though :( l.AddFirst(o); int hashCode = o.GetHashCode(); n++; if (hashCodesSeen.ContainsKey(hashCode)) { // Same hashCode seen twice for DIFFERENT

Equals vs GetHashCode when comparing objects

我只是一个虾纸丫 提交于 2019-11-26 20:57:29
问题 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() {

Good GetHashCode() override for List of Foo objects respecting the order

对着背影说爱祢 提交于 2019-11-26 18:52:15
EnumerableObject : IEnumerable<Foo> wraps a List<Foo> If EnumerableObject a.SequenceEquals( EnumerableObject b) , then they are equal. Therefore, a GetHashCode must be implemented. The problem is XORing each element in the list will return the same hash code for any list with all and only the same elements, regardless of order. This is Okay in terms of it working, but will result in many collisions, which will slow down retrieval, etc. What is a good, fast GetHashCode method for lists of objects that is order dependent? I'd do it the same way I normally combine hash codes - with an addition