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

Will converting Char.GetNumericValue() to int destroy information?

I am implementing GetHashCode(). I want to sum the values in a property called string Id then divide by some constant and return the result. I am using GetNumericValue(): int sum = 0; foreach (var ch in Id) sum += char.GetNumericValue(ch); But…
avivgood2
  • 227
  • 3
  • 19
2
votes
0 answers

IEquatable with GetHashCode?

I have created a Graph Class including HashSet as Nodes and HashSet> as Edges. public interface IEdge { T A_Node { get; } T Another_Node { get; } } public interface IGraph { ISet Nodes { get; } ISet>…
Parsa
  • 7,995
  • 2
  • 27
  • 37
2
votes
4 answers

Overriding the Equals and GetHashCode of a type, which has 'dibs'?

This question and Jon's answer made me aware this even existed, so I got curious and launched Visual Studio. I followed along one example of the MSDN page, and then I created my own little example. It's as follows: public class Person :…
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
2
votes
0 answers

Explanation for nested decimals behaviour in ValueType GetHashCode/Equals implementation

I was scratching my head over an unexpected failing comparison between two instances of a custom struct. I hope someone can either point me to reference source or documentation, or confirm whether this is a bug! Distilling the issue gets something…
Oly
  • 2,329
  • 1
  • 18
  • 23
2
votes
4 answers

GetHashCode() override coliding way to often

I'm using unity, and unity does not have a tuple in it, so I created my own tuple class to work since I needed it for my Dictionary. Dictionary , Tile> Tile class that I created and isn't really relevant to solve this problem(at…
2
votes
4 answers

Why is the type of 'base' object my concrete type?

It seems a very simple question, but clearly I'm missing something. I did a test: class A { public override int GetHashCode() { Console.WriteLine(base.GetType().Name); return 0; } } I was expecting to find 'object' but I…
Phate01
  • 2,499
  • 2
  • 30
  • 55
2
votes
1 answer

Proper way to write GetHashCode() when Equality Comparer is based on OR operation?

I'm trying to write an Equality Comparer for a simple class with 3 fields, like so: public class NumberClass { public int A { get; set; } public int B { get; set; } public int C { get; set; } } My condition for two objects of…
Sach
  • 10,091
  • 8
  • 47
  • 84
2
votes
1 answer

Same structs have different HashCode

I have a code: public class Point { public int x; public int y; public Point() { x = 0; y = 0; } public Point(int a, int b) { x = a; y = b; } } public struct Coefficients{ public double a; …
2
votes
4 answers

Double.GetHashCode algorithm or override

i have an application project that both managed and unmanaged code runs and i need to use the same algorithm for hashing double values in both systems. so either i will override System.Double.GetHashCode() or use its algorithm in c++ code. i could…
ali_bahoo
  • 4,732
  • 6
  • 41
  • 63
2
votes
1 answer

wpf overriding getHashCode and Eqaul in ContentControl

Hi I have a class which derives from ContentControl and I'm not able to override GetHashCode and Equal method. I get an error Error 5 cannot override inherited member 'System.Windows.DependencyObject.GetHashCode()' because it is sealed Is there…
pieere
  • 141
  • 1
  • 4
2
votes
1 answer

SequenceEqual in Equals makes GetHashCode broken

(1) I know that GetHashCode must return the same number for two objects if they are equal. (2) I also know that SequenceEqual compare each value of a List, and Equals(list1, list2) will return true only if list1 and list2 are the same instance.…
ZwoRmi
  • 1,093
  • 11
  • 30
2
votes
2 answers

Overriding GetHash code and Equals break bindings

Currently i have the problem, that when i override GetHashCode and Equals, my binding get's broken. This are my models: class A { private string name; public string Name { get { return name; } set { name = value; /*Some…
BendEg
  • 20,098
  • 17
  • 57
  • 131
2
votes
3 answers

How to impelement GetHashCode in class without any ID

For example I have class class Person { public string FirstName { get; set; } public string LastName { get; set; } } How to override GetHashCode in this class?
Grigor Aleksanyan
  • 540
  • 1
  • 6
  • 19
2
votes
3 answers

Is it safe to override GetHashCode and get it from string property?

I have a class: public class Item { public string Name { get; set; } public override int GetHashCode() { return Name.GetHashCode(); } } The purpose of overriding GetHashCode is that I want to have only one occurence of an…
Szybki
  • 1,083
  • 14
  • 29
2
votes
1 answer

GetHashCode of System.Type returns different values

Why does GetHashCode returns different values for the same type. If i execute this code: Console.WriteLine(typeof(Guid).GetHashCode()); In different applications, i get different hash codes. If i execute the following statement multiple times in…
BendEg
  • 20,098
  • 17
  • 57
  • 131