I have a class that looks like this.
public class Point : IEquatable<Point>
{
public int _xValue {get;set;}
public double _yValue {get;set;}
public Point(int x, double y)
{
_xValue = x;
_yValue = y;
}
public override bool Equals(object obj)
{
return Equals(obj as Point);
}
public bool Equals(Point obj)
{
return obj != null && obj._xValue == this._xValue && obj._yValue == this._yValue;
}
}
I have to implement the GetHashCode
function so that I can use it as a dictionary key. But I'm not clear on what the GetHashCode
function has to return in this situation. Can someone help me out?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.
It should return an integer, preferably unique for each seperate instance of the object. A hash value is basically a single number created out of the contents of an object, used to uniquely identify that object. The number one rule is that if two of those Points evaluate as equal to each other, the hash value should be the same for both of them.
A more detailed description is available at MSDN
The GetHashCode function needs to return am integer that will uniquely identify one instance of the object from another so as to avoid collisions when it is used as a key in the dictionary.
You should be able to reliably reproduce the hash code so try and avoid using random or date values as seeds for the hash code.
You could do _xValue ^ _yValue.GetHashCode()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With