gethashcode

What is the best algorithm for an overridden System.Object.GetHashCode?

In .NET System.Object.GetHashCode method is use in a lot of places throughout the .NET base class libraries. Especially when finding items in a collection fast or to determine equality. Is there a standard algorithm/ best practise on how to implement the GetHashCode override for my custom classes so I don't degrade performance? ...

Default implementation for Object.GetHashCode()

Does anyone know or have an idea on how the default implementation for GetHashCode() works? And does it handle structures, classes, arrays, etc efficiently and well enough? Trying to decide under what cases I should pack my own and what cases I can safely rely on the default implementation to do well. Don't want to reinvent the wheel if...

.NET unique object identifier

Is there any way of getting an unique identifier of an instance? GetHashCode() is same for 2 references pointing to the same instance. However, 2 different instances can (quite early) get same hash code: Hashtable hashCodesSeen = new Hashtable(); LinkedList<object> l = new LinkedList<object>(); int n = 0; while (true) { ob...

GetHashCode Extension Method

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...

Do I need to override GetHashCode() on reference types?

I read most questions on StackOverflow with regards to GetHashCode. But I am still not sure whether I have to override GetHashCode on reference types. I picked up the following from someones answer in another question: Object.GetHashCode() uses an internal field in the System.Object class to generate the hash value. Each object ...

C# How to select a Hashcode for a class that violates the Equals contract?

I've got multiple classes that, for certain reasons, do not follow the official Equals contract. In the overwritten GetHashCode() these classes simply return 0 so they can be used in a Hashmap. Some of these classes implement the same interface and there are Hashmaps using this interface as key. So I figured that every class should at l...

Overriding GetHashCode for mutable objects? [C#]

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 w...

Equals method implementation helpers (C#)

Everytime I write some data class, I usually spend so much time writing the IEquatable implementation. The last class I wrote was something like: public class Polygon { public Point[] Vertices { get; set; } } Implementing IEquatable was exaustive. Surely C#3.0/LINQ helps a lot, but the vertices can be shifted and/or in the revers...

GetHashCode() problem using xor

My understanding is that you're typically supposed to use xor with GetHashCode() to produce an int to identify your data by its value (as opposed to by its reference). Here's a simple example: class Foo { int m_a; int m_b; public int A { get { return m_a; } set { m_a = value; } } public int B ...

How should I go about implementing Object.GetHashCode() for complex equality?

Basically, I have the following so far: class Foo { public override bool Equals(object obj) { Foo d = obj as Foo ; if (d == null) return false; return this.Equals(d); } #region IEquatable<Foo> Members public bool Equals(Foo other) { if (this.Guid != String.Empty && t...

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

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 nu...

How to override equals for specific NHibernate class

I am struggling to figure out how I should override equals and get hashcode for a class I am writing using NHibernate. Basic business scenario is that users cannot re-use the same password within a 90 day limit. So I have a "user" that has many "historical passwords"... the user class was easy as I just use the login in the equals. B...

Object.GetHashCode

...

How to implement a GetHashCode compatible Equals method, when the space is greater than 32 bits?

In .NET you need that Equals(object) and GetHashCode() are compatible. But sometimes you can't: public class GreaterThan32Bits { public int X { get; set; } public int Y { get; set; } } Because the data density is greater than 32 bits, and GetHashCode returns an Int32, you will have 3 solutions (assuming a correctly implemented...

Creating the GetHashCode method in C#

What is the best way to create your own GetHashCode method for a class in C#? Suppose I have a simple class (which overrides the Equals method), as follows: class Test { public string[] names; public double[] values; public override bool Equals(object obj) { return (obj is Test) && this.Equals((...

.NET equivalent to java.util.Arrays.hashCode() function for arrays of intrinsic types?

Is there a.NET utility class equivalent to java.util.Arrays.hashCode() for arrays of intrinsic types such as int[], short[], float[], etc.? Obviously I could write my own utility class but was trying to find one already available in the .NET framework. ...

Converting a Double to an Integer for GetHashCode in Delphi

Delphi 2009 added the GetHashCode function to TObject. GetHashCode returns an Integer which is used for hashing in TDictionary. If you want an object to work well in TDictionary, you need to override GetHashCode appropriately such that, in general, different objects return different integer hash codes. But what do you do for objects c...

ActiveRecord and GetHashCode fails if int is nullable.

There is a problem when the int is nullable on GetHashCode At the point of GetHashCode on ActiveRecord.tt there is a need for a nullable check. Something like this. <# if(tbl.PK.SysType=="int" && !tbl.PK.Nullable ){ #> public override int GetHashCode() { return this.<#=tbl.PK.CleanName #>; ...

Is there a complete IEquatable implementation reference?

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 IEqu...

Can I trace object identity using GetHashCode?

What is the use of GetHashCode()? Can I trace object identity using GetHashCode()? If so, could you provide an example? ...