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
1666
votes
22 answers

What is the best algorithm for overriding GetHashCode?

In .NET, the GetHashCode method is used in a lot of places throughout the .NET base class libraries. Implementing it properly is especially important to find items quickly in a collection or when determining equality. Is there a standard algorithm…
bitbonk
  • 48,890
  • 37
  • 186
  • 278
191
votes
7 answers

Default implementation for Object.GetHashCode()

How does the default implementation for GetHashCode() work? And does it handle structures, classes, arrays, etc. efficiently and well enough? I am trying to decide in what cases I should pack my own and in what cases I can safely rely on the default…
Fung
  • 7,530
  • 7
  • 53
  • 68
163
votes
3 answers

What's the role of GetHashCode in the IEqualityComparer in .NET?

I'm trying to understand the role of the GetHashCode method of the interface IEqualityComparer. The following example is taken from MSDN: using System; using System.Collections.Generic; class Example { static void Main() { try { …
Lucian
  • 3,981
  • 5
  • 30
  • 34
137
votes
11 answers

.NET unique object identifier

Is there a way of getting a unique identifier of an instance? GetHashCode() is the same for the two references pointing to the same instance. However, two different instances can (quite easily) get the same hash code: Hashtable hashCodesSeen = new…
Martin Konicek
  • 39,126
  • 20
  • 90
  • 98
126
votes
3 answers

Correct way to override Equals() and GetHashCode()

I have never really done this before so i was hoping that someone could show me the correct what of implementing a override of Except() and GetHashCode() for my class. I'm trying to modify the class so that i can use the LINQ Except()…
Nugs
  • 5,503
  • 7
  • 36
  • 57
71
votes
2 answers

Does String.GetHashCode consider the full string or only part of it?

I'm just curious because I guess it will have impact on performance. Does it consider the full string? If yes, it will be slow on long string. If it only consider part of the string, it will have bad performance (e.g. if it only consider the…
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
61
votes
5 answers

Overriding GetHashCode for mutable objects?

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…
Davy8
  • 30,868
  • 25
  • 115
  • 173
61
votes
3 answers

How do I create a HashCode in .net (c#) for a string that is safe to store in a database?

To quote from Guidelines and rules for GetHashCode by Eric Lippert: Rule: Consumers of GetHashCode cannot rely upon it being stable over time or across appdomains Suppose you have a Customer object that has a bunch of fields like Name, …
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
55
votes
5 answers

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

What's the best strategy for Equals and GetHashCode?

I'm working with a domain model and was thinking about the various ways that we have to implement these two methods in .NET. What is your preferred strategy? This is my current implementation: public override bool Equals(object obj) { var newObj…
tucaz
  • 6,524
  • 6
  • 37
  • 60
49
votes
11 answers

General advice and guidelines on how to properly override object.GetHashCode()

According to MSDN, a hash function must have the following properties: If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for…
Svish
  • 152,914
  • 173
  • 462
  • 620
45
votes
4 answers

Good GetHashCode() override for List of Foo objects respecting the order

EnumerableObject : IEnumerable wraps a List If EnumerableObject a.SequenceEquals( EnumerableObject b), then they are equal. Therefore, a GetHashCode must be implemented. The problem is XORing each element in the list will return the same…
Ben B.
  • 3,706
  • 5
  • 26
  • 27
43
votes
6 answers

Why might a System.String object not cache its hash code?

A glance at the source code for string.GetHashCode using Reflector reveals the following (for mscorlib.dll version 4.0): public override unsafe int GetHashCode() { fixed (char* str = ((char*) this)) { char* chPtr = str; int…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
39
votes
2 answers

Why do string hash codes change for each execution in .NET?

Consider the following code: Console.WriteLine("Hello, World!".GetHashCode()); First run: 139068974 Second run: -263623806 Now consider the same thing written in Kotlin: println("Hello, World!".hashCode()) First run: 1498789909 Second…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
38
votes
7 answers

C#: How would you unit test GetHashCode?

Testing the Equals method is pretty much straight forward (as far as I know). But how on earth do you test the GetHashCode method?
Svish
  • 152,914
  • 173
  • 462
  • 620
1
2 3
22 23