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
3
votes
2 answers

How to get original string from the int generated by gethashcode

I have generate a hash code as string textBoxVal="Naresh"; int code =textBoxVal.GetHashCode(); textBox2.Text=code.ToString(); It has generated a integer value as -1078339947; Now I want to get the Original name Naresh with this(-1078339947)…
Learner
  • 1,286
  • 6
  • 34
  • 57
3
votes
3 answers

What is wrong with my List.Distinct()?

I have a class MyItems that implements IEqualityComparer and overrides the following methods: public bool Equals(MyItems item1, MyItems item2) { return (item1.ID == item2.ID && item1.itemName.Equals(item2)); } public int GetHashCode(MyItems…
B-Rad
  • 1,519
  • 5
  • 17
  • 32
3
votes
1 answer

GetHashCode is implemented but Dictionary is unable to find the key?

In my class, I have implemented Equals and GetHashCode. yet when I use it as key for a dictionary in my C# code, I get the error : "Key not found exception" Thanks, public class Time: IEquatable
Saeid Farivar
  • 1,667
  • 24
  • 43
3
votes
2 answers

Should GetHashCode() return value be based on original object's state or the modified object's state?

I've asked this question recently a few different ways, but don't get an answer that tells me how a Dictionary of needs to be handled when I hold a reference to something that changes T.GetHashCode(). For the purpose of this question "state"…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
3
votes
2 answers

Why do we need GetHashCode() function in the Object Model Project?

Possible Duplicate: Why is it important to override GetHashCode when Equals method is overriden in C#? I was looking into the following class in my Object Model and could not understand the significance of adding GetHashCode() in the…
Pankaj
  • 9,749
  • 32
  • 139
  • 283
3
votes
1 answer

C# GetHashCode() High Performance Hashing Algorithm

Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? This is known to us that if we override the Equals method of Object in our custom types, we should also override and provide an implementation of…
S2S2
  • 8,322
  • 5
  • 37
  • 65
2
votes
1 answer

NHibernate, non composite ID and GetHashCode

When my domain model have a composite key in the database I will get an exception when forgetting to override Equals/GetHashCode NHibernate.MappingException: composite-id class must override Equals(): Why does it not give me the same error when I…
Stig
  • 1,974
  • 2
  • 23
  • 50
2
votes
6 answers

Random numbers vs. GetHashCode() in CompreTo()?

I'm using the Random class in my struct's CompareTo() to pick, with equal probability, one of the structs when both have the same field values. The Random class is instantiated with a fixed seed to get a reproducible sequence of pseudo-random…
alhazen
  • 1,907
  • 3
  • 22
  • 43
2
votes
5 answers

Converting Object.GetHashCode() to Guid

I need to assign a guid to objects for managing state at app startup & shutdown It looks like i can store the lookup values in a dictionary using dictionary.Add(instance.GetHashCode(), myGUID()); are there any potential issues to be…
Kumar
  • 10,997
  • 13
  • 84
  • 134
2
votes
1 answer

Does overriding the GetHashCode method mean used values should never change?

If I have a class with properties public class a{ public int b; public int c; public int d; } And I override the GetHashCode method in the class with public class a { public int b; public int c; public int d; public…
rick
  • 23
  • 4
2
votes
2 answers

C#: override GetHashCode, what does this code do?

Here's the code I found in the Nhibernate 3 Beginners Guide to override GetHashCode. I don't understand why it uses result * 397. If 397 just a random number he use to generate unique result?? Can we just GetHashCode for firstname, middlename and…
qinking126
  • 11,385
  • 25
  • 74
  • 124
2
votes
3 answers

Does this solve Nhibernate identity problem and GetHashCode issues?

The solution I propose involves quite a bit of code, but you can just copy it all and past it in a VS test solution assuming you have SqLite installed, and you should be able to run the tests yourself. As I have been struggling with the object…
halcwb
  • 1,480
  • 1
  • 13
  • 26
2
votes
1 answer

How to find a string with specific letters in gethash javascript function

function getHash( string ) { let h = 7, letters = "acdefhlmnoprstuw"; for( var i = 0; i < string.length; i++ ) { h = ( h * 37 + letters.indexOf( string[ i ] ) ); } return h; } My task is to write a code which finds…
Vkx
  • 33
  • 5
2
votes
2 answers

GetHashCode for a pair of playing cards C#

I have a playing card class with the GetHashCode method implemented as follows: public override int GetHashCode() { return (int)rank * 4 + (int)suit; } I now want to design a GetHashCode method that works for a 2 card hand/pair of cards. I…
Steve W
  • 1,108
  • 3
  • 13
  • 35
2
votes
2 answers

How to implement the override methods of Equals and GetHashCode in a class that implements IProxyGenerationHook in Castle.Core?

Reading the Castle.Core documentation, in this link, they recommend that always overriding the Equals and GetHashCode methods of the classes that implement IProxyGenerationHook. I have a class called MiHook that implements such interface, but this…
Marlonchosky
  • 494
  • 5
  • 16