Can anyone of u please explain about getHashCode().Its really very hard to understand.
Is there any site with good explanation about getHashCode() and Icollector?
Can anyone of u please explain about getHashCode().Its really very hard to understand.
Is there any site with good explanation about getHashCode() and Icollector?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
jaguRitaPosted Aug 9, 2007, 1:24 AM
Thanks Mike...............
U guys r Great!!!
Mike GoldPosted Aug 8, 2007, 11:48 PM
This is the basics of GetHashCode as it applies to .NET:
1) the default implementation (for an object instance) does not work so don't use it.
2) GetHashCode should be overriden to produce a unique key for an instance of an object that is unique as defined by the Equals method. In other words, if the Equals method returns true for two objects of the same class, then they should generate the same hashcode with GetHashCode. If the Equals method returns false for two objects of the same class, then GetHashCode should return two different hashcodes.
3) The Equals method should be overriden to return a boolean indicating if two objects of the same class are equal. You get to determine what equality means. For example, for the Point class, two points are equal if they have the same x and y values. Two instances are also equal if they have the same reference (meaning they are the same instance).
4)The String class already has GetHashCode and Equals implemented. That said, If you can generate a unique string from your class instance, a quick way to override the GetHashCode method is to call GetHashCode on the unique string.
e.g.
public override int GetHashCode()
{
string uniqueString = _member1.ToString() + "*" +
_member2.ToString() + "**";
return uniqueString.GetHashCode();
}
(Some argue though, that the process of concatenation is also expensive, so you can experiment with other ways to generate unique integers for your class such as using exclusive or)
You can create your override Equals method to reflect the members of your class you used to generate your unique string.
bool Equals(object otherObject)
{
return (_member1 == ((MyClass)otherObject)._member1) &&
(_member2 == ((MyClass)otherObject)._member2)
}
For a point class, it might look something like this:
class Point
{
public int X;
public int Y;
public override int GetHashCode()
{
string uniqueString = _X.ToString() + "*" +
Y.ToString() + "**";
return uniqueString.GetHashCode();
}
bool Equals(object otherObject)
{
return (X == ((Point)otherObject).X) &&
(Y == ((Point)otherObject).Y)
}
}
So Why does a class need to have unique objects and hashcodes? Because if you use the Hashtable or Dictionary collections, these collections depend upon unique keys. Therefore these classes need a way to determine uniqueness for a key. Equals works for this, but of course, but it may be much faster to compare 2 integer values that represent uniqueness. That is where GetHashCode comes in.
You see the way Hashtables work is that they store the data in buckets. The Hashtable class uses GetHashCode to quickly determine through its lookup, which bucket the value lives in and within this bucket it will use Equals to compare objects. This significantly improves the speed of your lookup.
Hope this helps!