[Mono-list] mcs compiles on linux. Now what?

Paolo Molaro lupus@ximian.com
Fri, 8 Mar 2002 12:21:56 +0100

On 03/07/02 Piers Haken wrote:
> Yes, Object.GetHashCode() must be based on immutable instance values
> (otherwise you lose objects in hashtables). But the kicker is that if
> Object.Equals() returns true then the two object's GetHashCode()s _must_
> be the same !!!

I now remember I saw that discussion. The hack I used yesterday was to
use the address of the object as the hash key: this works nicely  for
class types until we have a moving garbage collector:-)
I planned to write a proper solution today, but since there are issues
on what the 'proper solution' could be, I'm keeping my current hack
without committing it to cvs (it's included at the end of the mail for
the courious).

The right solution should probably involve calculating the hash only
over valuetype fields in the object, skipping object references (that
can vary with a moving collector). This could be the default behaviour,
more complicated stuff must be handled on a per-type basis.


lupus@debian.org                                     debian/rules
lupus@ximian.com                             Monkeys do it better

Index: System/Object.cs
RCS file: /cvs/public/mcs/class/corlib/System/Object.cs,v
retrieving revision 1.13
diff -u -r1.13 Object.cs
--- System/Object.cs	2002/03/07 22:59:52	1.13
+++ System/Object.cs	2002/03/08 07:25:51
@@ -63,7 +63,7 @@
 		// </summary>
 		public virtual int GetHashCode ()
-			return 0;
+			return (int)obj_address ();
 		// <summary>