Questions tagged [gethashcode]

GetHashCode is method of base Object class of .Net Framework.

GetHashCode is method of base Object class of .Net Framework. A hash code is a numeric value that is used to identify an object during equality testing. It can also serve as an index for an object in a collection. The GetHashCode method is suitable for use in hashing algorithms and data structures such as a hash table.

Important notes

  • Equality of GetHashCode value doesn't necessarily imply equality of objects. The equality must be verified using Equals.
  • Inequality of GetHashCode necessarily means inequality of objects.
344 questions
8
votes
2 answers

Overriding Equality Operators

I've implemented a class that overloads the == and != operators. This seems to work fine; however, I get the warning 'type' defines operator == or operator != but does not override Object.Equals(object o). Okay, so I implemented Equals. But now I…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
8
votes
5 answers

Hibernate n:m extractHashCode throws NullPointerException

I get the following exception while inserting an object with hibernate. Reading from the database works like a charm. I use MySQL 5.5 as database provider and hibernate 3.6.5. I have the following database…
Florian
  • 93
  • 1
  • 7
8
votes
3 answers

Have I implemented Equals()/GetHashCode() correctly?

The program was working with this implementation: class Instrument { public string ClassCode { get; set; } public string Ticker { get; set; } public override string ToString() { return " ClassCode: " + ClassCode + " Ticker: "…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
8
votes
3 answers

C# GetHashCode question

What would be the best way to override the GetHashCode function for the case, when my objects are considered equal if there is at least ONE field match in them. In the case of generic Equals method the example might look like this: public bool…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
8
votes
1 answer

How to pick prime numbers to calculate the hash code?

This question follows on the answer given by Jon Skeet on the question: "What is the best algorithm for an overridden System.Object.GetHashCode?". To calculate the hash code the following algorithm is used: public override int GetHashCode() { …
Joost Verbraeken
  • 883
  • 2
  • 15
  • 30
8
votes
5 answers

Why returning false ? new Person("james") == new Person("james")?

I have override GetHashCode and Equals and both methods provide same results for different objects but why still getting false ? class Program { static void Main(string[] args) { Console.WriteLine(new Person("james") == new…
Freshblood
  • 6,285
  • 10
  • 59
  • 96
7
votes
1 answer

Substitute the GetHashCode() Method of System.Drawing.Point

System.Drawing.Point has a really, really bad GetHashCode method if you intend to use it to describes 'pixels' in a Image/Bitmap: it is just XOR between the X and Y coordinates. So for a image with, say, 2000x2000 size, it has an absurd number of…
Trauer
  • 1,981
  • 2
  • 18
  • 40
7
votes
2 answers

What is the best way for calculating hashcode of a class with string properties?

I have a class with string properties and I need to override GetHashCode() method. class A { public string Prop1 { get; set; } public string Prop2 { get; set; } public string Prop3 { get; set; } } The first idea is to do something like…
Warlock
  • 7,321
  • 10
  • 55
  • 75
6
votes
2 answers

how to implement override of GetHashCode() with logic of overriden Equals()

I have some classes as below, i have implemented the Equals(Object) method for almost all of them. But i don't know how to write GetHashCode() . As far I used these data types as value type in a Dictionary Collection, i think i should override…
rene
  • 156
  • 1
  • 2
  • 11
6
votes
3 answers

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

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…
Joe
  • 964
  • 1
  • 10
  • 27
6
votes
1 answer

Implementation of Dictionary where equivalent contents are equal and return the same hash code regardless of order of insertion

I need to use Dictionary collections that given two instances d1 and d2 where they each have the same KeyValuePair contents, which could be inserted in any order: (d1 == d2) evaluates to true d1.GetHashCode() ==…
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
6
votes
3 answers

Should GetHashCode be implemented for IEquatable on mutable types?

I'm implementing IEquatable, and I am having difficulty finding consensus on the GetHashCode override on a mutable class. The following resources all provide an implementation where GetHashCode would return different values during the object's…
Neo
  • 4,145
  • 6
  • 53
  • 76
6
votes
1 answer

GetHashCode good practice?

For a Delphi project (built with RAD Studio XE7), I want to create a dictionary of brushes. Each dictionary item contains a TMyBrush object as key, that describes the brush to retrieve, and a GDI+ brush as value. The TMyBrush class contains 3…
Jean-Milost Reymond
  • 1,833
  • 1
  • 15
  • 36
6
votes
2 answers

Value vs. Reference equality in generic List.Contains()

Attempt #3 to simplify this question: A generic List can contain any type - value or reference. When checking to see if a list contains an object, .Contains() uses the default EqualityComparer for type T, and calls .Equals() (is my…
James King
  • 6,233
  • 5
  • 42
  • 63
6
votes
4 answers

Hash code non-zero initial value - note: I am not asking about primes

This is kind of an academic point, but I feel I don't fully understand hash codes if I don't understand why this is recommended by books such as Effective Java and many SO questions. Suppose: public sealed class Point { private readonly int x; …
weston
  • 54,145
  • 21
  • 145
  • 203