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

What to return when overriding Object.GetHashCode() in classes with no immutable fields?

Ok, before you get all mad because there are hundreds of similar sounding questions posted on the internet, I can assure you that I have just spent the last few hours reading all of them and have not found the answer to my…
Sheridan
  • 68,826
  • 24
  • 143
  • 183
12
votes
9 answers

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…
user65199
11
votes
2 answers

Library with Equals and GetHashCode helper methods for .NET

Google Guava provides nice helpers to implement equals and hashCode like the following example demonstrates: public int hashCode() { return Objects.hashCode(lastName, firstName, gender); } Is there a similar library for Microsoft .NET?
deamon
  • 89,107
  • 111
  • 320
  • 448
11
votes
5 answers

Why use GetHashCode() over Equals()?

HashSet.Add first compares the results of GetHashCode. If those are equal, it calls Equals. Now, my understanding is in order to implement GetHashCode, something must be done with the fields of an object. A simple example implementation can be…
user247702
  • 23,641
  • 15
  • 110
  • 157
11
votes
1 answer

How are Equals and GetHashCode implemented on anonymous types?

The Help says this: Anonymous types are class types that derive directly from object, and that cannot be cast to any type except object. The compiler provides a name for each anonymous type, although your application cannot access it. From…
david.pfx
  • 10,520
  • 3
  • 30
  • 63
11
votes
7 answers

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 …
Jon B
  • 51,025
  • 31
  • 133
  • 161
10
votes
4 answers

C# .NET GetHashCode function question

Hi I have a class with 6 string properties. A unique object will have different values for atleast one of these fields To implement IEqualityComparer's GetHashCode function, I am concatenating all 6 properties and calling the GetHashCode on the…
ganeshran
  • 3,512
  • 7
  • 41
  • 69
10
votes
8 answers

Using GetHashCode to test equality in Equals override

Is it ok to call GetHashCode as a method to test equality from inside the Equals override? For example, is this code acceptable? public class Class1 { public string A { get; set; } public string B { get; set; } …
Armbrat
  • 2,295
  • 1
  • 23
  • 32
10
votes
1 answer

Is it safe to use GetHashCode to compare identical Anonymous types?

Given two identical anonymous type objects: {msg:"hello"} //anonType1 {msg:"hello"} //anonType2 And assume that they haven't resolved to the same type (e.g. they might be defined in different assemblies) anonType1.Equals(anonType2);…
James Wiseman
  • 29,946
  • 17
  • 95
  • 158
10
votes
3 answers

How should we really be implenting Equals and GetHashCode for NHibernate entities

There are many questions and answers and articles to this question available but in my opinion there seems to be no real clear/correct answer For me Ayende has the best generic implementation so far that I've seen :…
Tom Carter
  • 2,938
  • 1
  • 27
  • 42
10
votes
2 answers

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
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
9
votes
1 answer

GetHashCode for a class with a list object

I have such a class: public class Cycle { public List Edges { get; private set; } public override bool Equals(object obj) { Cycle cycle = (Cycle)obj; …
Graviton
  • 81,782
  • 146
  • 424
  • 602
9
votes
1 answer

C# Decimal.GetHashCode() and Double.GetHashCode() equal

Why is it that 17m.GetHashCode() == 17d.GetHashCode() (m=decimal, d=double) Additionally, as expected 17f.GetHashCode() != 17d.GetHashCode() (f=float) This appears to be true for both net3.5 and net4.0. As I understand, the internal bit…
Roland Pihlakas
  • 4,246
  • 2
  • 43
  • 64
9
votes
4 answers

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…
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
8
votes
2 answers

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…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625