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
0
votes
0 answers

Hashcodes - multiply or xor?

I saw this hash function - and it triggered some alarms: public override int GetHashCode() { var result = 0; unchecked { result = anIntId.GetHashCode(); result *= 397 * (aString != null ? aString.GetHashCode() : 0); } …
blueberryfields
  • 45,910
  • 28
  • 89
  • 168
0
votes
1 answer

C# Dictionary Key override not finding key

I am trying to search through a dictionary with TryGetValue using an object as a key. I have overridden the GetHashCode which I thought would be what was required to set how the key is generated for a dictionary. The Item class below is the key for…
Euthyphro
  • 700
  • 1
  • 9
  • 26
0
votes
1 answer

Get hash code and exception

Recently I have asked a question about GetHashCode GetHashCode for the object with several data members After checking http://msdn.microsoft.com/en-us/library/ms132155(v=vs.110).aspx, looks that if the input for GetHashCode is null the exception…
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
0
votes
1 answer

using long (int64) as a hashCode and still use IEqualityComparer for concurrent Dictionary

I have a problem using a self made IEqualityComparer and GetHashCode in a concurrent dictionary. The class below (simplified with used two properties) works perfect when I implement it like this: ConcurrentDictionary
user369122
  • 792
  • 3
  • 13
  • 33
0
votes
1 answer

GetHashCode for the object with several data members

public class MyClass { public string x; public string y; } public class MyClassEqualityComparer : IEqualityComparer { public int GetHashCode(MyClass myobj) { if(myObj == null) { return…
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
0
votes
2 answers

GetHashCode() behaviour work when Dictionary has more than Int.MaxValue elements

GetHashCode() returns a int32 as hash. I was wondering how would it work when the number of elements exceed int.MaxValue, as all of them would have returned some integer <= int.MaxValue?
0
votes
2 answers

Convert String to Int (NOT PARSE)

How can i get the numeric representation of a string in C#? To be clear, I do not want the address of the pointer, I do not want to parse an int from a string, I want the numeric representation of the value of the string. The reason I want this…
Nealon
  • 2,213
  • 6
  • 26
  • 40
0
votes
2 answers

Definition of GetHashCode() in C#

Dictionary in C# uses GetHashCode() to retrieve hash code of a given key. I walk through whole of the Dictionary Class, but there is not any definition for GetHashCode() function. It is bothering me and I don't know how I can find definition of it…
Chavoosh
  • 425
  • 4
  • 15
0
votes
1 answer

GetHashCode different result on Debug and Unit Test running

Today I wanna test my critical program with C#. In this program some properties will hash via GetHashCode() method. After implement parts of program, Unit Test project was added. Then I run the program in debug mode and copy hashed data to notepad…
Ahmad
  • 437
  • 5
  • 20
0
votes
0 answers

GetHashCode().ToString() returns null

I get a strange inconsistent server error that is occasionally being raised by a code running when ASP.net web site is loaded. The exception is a Null reference exception and it happens because somehow the following code (specifically written in…
0
votes
0 answers

add Equality Comparer class to base class for custom property classes in c#

i'm using the ConcurrentDictionary were the key is made of a class with public properties. after playing around with the code from (HashCode on decimal with IEqualityComparer in a ConcurrentDictionary) I wanted to find a solution, so I don't have to…
0
votes
2 answers

How can I identify a bad implementation of GetHashCode?

I have an implementation of GetHashCode which I believe to be fairly robust, but, honestly, I dredged it up from the depths of the internet and, while I understand what is written, I don't feel qualified to describe it as a 'good' or a 'bad'…
Sean Anderson
  • 27,963
  • 30
  • 126
  • 237
0
votes
2 answers

C# - How to override GetHashCode with Lists in object

I am trying to create a "KeySet" to modify UIElement behaviour. The idea is to create a special function if, eg. the user clicks on an element while holding a. Or ctrl+a. My approach so far, first lets create a container for all possible modifiers.…
Christian Ruppert
  • 3,749
  • 5
  • 47
  • 72
0
votes
2 answers

Is it a bad idea creating an object in a GetHashCode function?

I have a base class MyBase and about a dozen of derived classes. I rely on the visitor pattern heavily in my code. So the base class is an abstract host for a visitor, each of the derived classes is a concrete host. I am using a standalone comparer…
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
0
votes
0 answers

HashCode on decimal with IEqualityComparer in a ConcurrentDictionary

I made a class to be used as a key in a dictionary. public class FourUintsOneDecimalKeyInfo { public uint IdOne { get; set; } public uint IdTwo { get; set; } public uint IdThree { get; set; } public uint IdFour { get; set; } …
user369122
  • 792
  • 3
  • 13
  • 33