T
Tony Johansson
Hello!
First some background information before I ask the question.The text is just
copied from a book that I'm reading
The .Net Framework doesn't always understand equality as we do, however. For
example, imagine you created class Fish
that holds the name of the fish like below.
public class Fish
{
string name;
public Fish(string theName)
{
name = theName;
}
}
Now if we create two instances of the Fish class with the same name, the
Hashtable treats them as different objects, as shown in the following code
example.
Hashtable duplicates = new Hashtable();
Fish key1 = new Fish("Herring");
Fish key2 = new Fish("Herring");
duplicates[key1] = "Hello";
duplicates[key2] = "Hello";
Console.WriteLine(duplicates.Count); // give 2
The reason we have two items in the collection is because the Object class's
implementation of GetHashCode creates a hash that is likely to be unique for
each instance of a class. I can override the GetHashCode in the Fish class
to try and let the Hashtable known they are equal.
Here is the code that override the GetHashCode in class Fish
public override int GetHashCode()
{
return name.GetHashCode();
}
If I return the hash of the fish's name, the two instances of the fish will
have the same hash code.
But is that enough for the Hashtable to determine they are identical
objects. Unfortunately, no. If the Hashtable find two objects with the same
hash, it calls their Equals method to see whether the two objects are in
fact equal. Again, the default implementation of Objects.Equals will return
false if the two objects are two different instances of the same class. So
we need to also add an override of the Equals method to our Fish class.
Now to my question: I mean that if the hash code is the same as in my
example then the object are equal so I don't understand why the framework
must call the equals.
Can somebody give me an example when you have the same hashcode but the
object is not equal.
//Tony
First some background information before I ask the question.The text is just
copied from a book that I'm reading
The .Net Framework doesn't always understand equality as we do, however. For
example, imagine you created class Fish
that holds the name of the fish like below.
public class Fish
{
string name;
public Fish(string theName)
{
name = theName;
}
}
Now if we create two instances of the Fish class with the same name, the
Hashtable treats them as different objects, as shown in the following code
example.
Hashtable duplicates = new Hashtable();
Fish key1 = new Fish("Herring");
Fish key2 = new Fish("Herring");
duplicates[key1] = "Hello";
duplicates[key2] = "Hello";
Console.WriteLine(duplicates.Count); // give 2
The reason we have two items in the collection is because the Object class's
implementation of GetHashCode creates a hash that is likely to be unique for
each instance of a class. I can override the GetHashCode in the Fish class
to try and let the Hashtable known they are equal.
Here is the code that override the GetHashCode in class Fish
public override int GetHashCode()
{
return name.GetHashCode();
}
If I return the hash of the fish's name, the two instances of the fish will
have the same hash code.
But is that enough for the Hashtable to determine they are identical
objects. Unfortunately, no. If the Hashtable find two objects with the same
hash, it calls their Equals method to see whether the two objects are in
fact equal. Again, the default implementation of Objects.Equals will return
false if the two objects are two different instances of the same class. So
we need to also add an override of the Equals method to our Fish class.
Now to my question: I mean that if the hash code is the same as in my
example then the object are equal so I don't understand why the framework
must call the equals.
Can somebody give me an example when you have the same hashcode but the
object is not equal.
//Tony