How to use System.HashCode.Combine with more than 8 values?

As stated in the System.HashCode documentation, adding together hashes returned by successive HashCode.Combine calls is NOT the solution. While the static HashCode.Combine method overloads only allow up to 8 values, these are just convenience methods – to combine more, instantiate the HashCode class itself and use it as follows: public override int GetHashCode() { HashCode … Read more

Why do 2 delegate instances return the same hashcode?

Easy! Since here is the implementation of the GetHashCode (sitting on the base class Delegate): public override int GetHashCode() { return base.GetType().GetHashCode(); } (sitting on the base class MulticastDelegate which will call above): public sealed override int GetHashCode() { if (this.IsUnmanagedFunctionPtr()) { return ValueType.GetHashCodeOfPtr(base._methodPtr); } object[] objArray = this._invocationList as object[]; if (objArray == null) … Read more

How can I determine equality for two JavaScript objects?

Why reinvent the wheel? Give Lodash a try. It has a number of must-have functions such as isEqual(). _.isEqual(object, other); It will brute force check each key value – just like the other examples on this page – using ECMAScript 5 and native optimizations if they’re available in the browser. Note: Previously this answer recommended Underscore.js, … Read more

Is it proper for equals() to depend only on an ID?

Whether you should do this depends on the semantics of your class. That is, what does it mean to say that two objects of your class are equivalent? The most important distinction is between objects with value semantics and objects with entity semantics. Entity objects are not equivalent even if they have equivalent attributes (colour, … Read more

Java: A “prime” number or a “power of two” as HashMap size?

Using a power of two effectively masks out top bits of the hash code. Thus a poor-quality hash function might perform particularly badly in this scenario. Java’s HashMap mitigates this by mistrusting the object’s hashCode() implementation and applying a second level of hashing to its result: Applies a supplemental hash function to a given hashCode, … Read more

Generate two different strings with the same hashcode

Finding two strings by repeatedly comparing random strings will take practically forever. Instead generate strings and store them in an a dictionary by hashcode. Then look up each. Match found pretty quickly. MATCH FOUND!! xqzrbn and krumld hash code 80425224 void Main() { var lookup = new Dictionary<int,string>(); while(true) { var s = RandomString(); var … Read more

tech