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
36
votes
6 answers

Overriding GetHashCode

As you know, GetHashCode returns a semi-unique value that can be used to identify an object instance in a collection. As a good practice, it is recommended to override this method and implement your own. My question is - do you override this method…
Den
  • 16,686
  • 4
  • 47
  • 87
35
votes
2 answers

String.GetHashCode() returns different values

Why is GetHashCode() returning a different value for the same string? I can't describe how to duplicate this, but trust that this is not a practical joke and that the two following lines came from my watch window at two separate…
Mark J Miller
  • 4,751
  • 5
  • 44
  • 74
31
votes
7 answers

Using GetHashCode for getting Enum int value

I have an enum public enum INFLOW_SEARCH_ON { ON_ENTITY_HANDLE = 0, ON_LABEL = 1, ON_NODE_HANDLE = 2 } // enum INFLOW_SEARCH_ON I have to use this enum for searching in a grid column. To get the column…
Mohit Vashistha
  • 1,824
  • 3
  • 22
  • 49
29
votes
7 answers

Overriding GetHashCode in VB without checked/unchecked keyword support?

So I'm trying to figure out how to correctly override GetHashCode() in VB for a large number of custom objects. A bit of searching leads me to this wonderful answer. Except there's one problem: VB lacks both the checked and unchecked keyword in…
Kumba
  • 2,390
  • 3
  • 33
  • 60
29
votes
4 answers

implement GetHashCode() for objects that contain collections

Consider the following objects: class Route { public int Origin { get; set; } public int Destination { get; set; } } Route implements equality operators. class Routing { public List Paths { get; set; } } I used the code below to…
Joanna Derks
  • 4,033
  • 3
  • 26
  • 32
27
votes
3 answers

What should GetHashCode return when object's identifier is null?

Which of the following is correct/better, considering that identity property could be null. public override int GetHashCode() { if (ID == null) { return base.GetHashCode(); } return ID.GetHashCode(); } OR public override int…
Dienekes
  • 1,548
  • 1
  • 16
  • 24
27
votes
5 answers

Why is ValueType.GetHashCode() implemented like it is?

From ValueType.cs **Action: Our algorithm for returning the hashcode is a little bit complex. We look ** for the first non-static field and get it's hashcode. If the type has no ** non-static fields, we return the hashcode of the…
alh84001
  • 1,263
  • 1
  • 15
  • 25
25
votes
4 answers

What is the difference between using IEqualityComparer and Equals/GethashCode Override?

When i am using dictionaries sometimes I have to change the default Equals meaning in order to compare Keys. I see that if I override the Equals and GetHashCode on the key's class or i create a new class which implements IEqualityComparer I have…
dariush624
  • 343
  • 1
  • 4
  • 9
24
votes
1 answer

How to use System.HashCode.Combine with more than 8 values?

.NET Standard 2.1 / .NET Core 3 introduce System.HashCode to quickly combine fields and values to a hash code without having to care about the underlying implementation. However, it only provides Combine method overloads for up to 8 values. What do…
Ray
  • 7,940
  • 7
  • 58
  • 90
23
votes
4 answers

Generate integer based on any given string (without GetHashCode)

I'm attempting to write a method to generate an integer based on any given string. When calling this method on 2 identical strings, I need the method to generate the same exact integer both times. I tried using .GetHasCode() however this is very…
mrb398
  • 1,277
  • 4
  • 24
  • 32
22
votes
2 answers

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

Why does C# not implement GetHashCode for Collections?

I am porting something from Java to C#. In Java the hashcode of a ArrayList depends on the items in it. In C# I always get the same hashcode from a List... Why is this? For some of my objects the hashcode needs to be different because the objects…
Peterdk
  • 15,625
  • 20
  • 101
  • 140
22
votes
2 answers

GetHashCode on null fields?

How do I deal with null fields in GetHashCode function? Module Module1 Sub Main() Dim c As New Contact Dim hash = c.GetHashCode End Sub Public Class Contact : Implements IEquatable(Of Contact) Public Name As String Public…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
21
votes
3 answers

C# how to calculate hashcode from an object reference

Folks, here's a thorny problem for you! A part of the TickZoom system must collect instances of every type of object into a Dictionary<> type. It is imperative that their equality and hash code be based on the instance of the object which means…
Wayne
  • 2,959
  • 3
  • 30
  • 48
20
votes
3 answers

Overriding GetHashCode()

In this article, Jon Skeet mentioned that he usually uses this kind of algorithm for overriding GetHashCode(). public override int GetHashCode() { unchecked // Overflow is fine, just wrap { int hash = 17; // Suitable nullity checks etc,…
Pacane
  • 20,273
  • 18
  • 60
  • 97
1
2
3
22 23