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
4
votes
5 answers

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,…
MB.
  • 7,365
  • 6
  • 42
  • 42
3
votes
5 answers

GetHashCode and Equals implementation in Dictionary C#

I came to this site searching for object comparison in Dictionary, and i came to know that overriding GetHashCode and Equals are a must for doing object comparison in C#. Here is a piece of code that i have been trying to solve out, using FOREACH…
3
votes
4 answers

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…
mafu
  • 31,798
  • 42
  • 154
  • 247
3
votes
3 answers

.NET: 64-bit Hash code

I need a 64-bit hash for strings, and the default .GetHashCode() returns only a 32-bit int. I could generate a MD5/SHA1 hash, and use only the first 64bits. But because those algorithms are cryptographically secure, they are much more demanding on…
Maestro
  • 9,046
  • 15
  • 83
  • 116
3
votes
1 answer

GetHashCode when one of properties can be null

I am currently trying to figure out how to deal with situation, where I want to use some object as a key in dictionary, therefore I need it to have overriden GetHashCode and Equals methods. The problem is that one of the properties of my object is…
Bruno Pfohl
  • 451
  • 1
  • 8
3
votes
2 answers

Should GetHashCode Depend on the Type?

Firstly, I'm using the GetHashCode algorithm described, here. Now, picture the following (contrived) example: class Foo { public Foo(int intValue, double doubleValue) { this.IntValue = intValue; this.DoubleValue =…
RichK
  • 11,318
  • 6
  • 35
  • 49
3
votes
1 answer

Override Equals and GetHashCode in class with one field

I have a class: public abstract class AbstractDictionaryObject { public virtual int LangId { get; set; } public override bool Equals(object obj) { if (obj == null || obj.GetType() != GetType()) …
user348173
  • 8,818
  • 18
  • 66
  • 102
3
votes
1 answer

Creating overload for String.GetHashCode() to return same value in x86 and x64 environments

I have a scenario in which my server side code runs in x86 environment where as the client side runs in x64. The issue is happening that we are using String.GetHashCode() to identify the objects within the HashTable since "The behavior of…
Manthan
  • 393
  • 2
  • 5
  • 13
3
votes
1 answer

How to include a "joker" value in HashCode

I have the following example class: public sealed class MyDictKey { public int Type { get; } public int SubType { get; } public MyDictKey(int type, int subType) // both can only be positive values { Type = type; …
Károly Ozsvárt
  • 1,025
  • 1
  • 12
  • 24
3
votes
0 answers

Why is Visual Studio using EqualityComparer on string when auto-generating GetHashCode

As of version 15.6.7, Visual Studio 2017 for the following class: class Contact { public int Id { get; set; } public string Name { get; set; } } generates this GetHashCode method (the numbers vary): public override int GetHashCode() { …
Josef Bláha
  • 1,033
  • 10
  • 21
3
votes
5 answers

How to write hash code for a tree-like data structure based on the location of the item in the tree?

Each item looks like this: public interface IEffect { string Name { get; } bool Compute ( ); List SubEffects { get; set; } IEffect ElseIfEffect { get; set; } } I want to create a tree-like structure using many instances of…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
3
votes
3 answers

Equals is used. GetHashCode is not

I have implemented the class below: public class carComparer : IEqualityComparer { public bool Equals(Car car1, Car car2) { if (car1 == null || car2 == null) return false; …
w0051977
  • 15,099
  • 32
  • 152
  • 329
3
votes
1 answer

GetHashCode for multiple boolean values

In the following StackOverflow question Jon Skeets answer specifies one good implementation is ... // Note: Not quite FNV! public override int GetHashCode() { unchecked // Overflow is fine, just wrap { int hash = (int) 2166136261; …
Gene S
  • 2,735
  • 3
  • 25
  • 35
3
votes
1 answer

Why can't I override GetHashCode on a many-to-many entity in EF4?

I have a many-to-many relationship in my Entity Framework 4 model (which works with a MS SQL Server Express): Patient-PatientDevice-Device. I'm using Poco, so my PatientDevice-class looks like this: public class PatientDevice { protected virtual…
Papa Mufflon
  • 17,558
  • 5
  • 27
  • 35
3
votes
2 answers

Why GetHashCode method needs to do shift in C#

According to MSDN GetHashCode Method: public struct Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public override bool Equals(Object obj) { if (!(obj…
Allen Chen
  • 228
  • 1
  • 8