# Friday, 01 November 2002
« Dependencies | Main | Conference »
GetHashCode & reflection

What is the output of the following code fragment?

  object o = "foo";
  Console.WriteLine(o.GetHashCode());
  Console.WriteLine(typeof(object).GetMethod("GetHashCode").Invoke(o, null));
  Console.WriteLine(typeof(string).GetMethod("GetHashCode").Invoke(o, null));

.NET 1.0 says:

193410979
23
193410979

.NET 1.1 beta says:

193410979
193410979
193410979

Mono 0.13 says:

101574
1554225471
1554225471

Mono 0.16 says:

101574
33369132
101574

Note: the actual numbers are irrelevant, it matters if they match up or not.

.NET 1.1 is correct, the others aren't. The funny thing is, I just realised that IK.VM.NET currently depends (for its System.identityHashCode implementation) on the broken behavior.

.NET 1.0 only behaves this way for methods in String, for other types reflective method invocation works the same as a virtual method call.

In Mono 0.16 all reflective method invocations appear to be non-virtual.

Friday, 01 November 2002 14:41:12 (W. Europe Standard Time, UTC+01:00)  #    Comments [5]
Saturday, 02 November 2002 10:06:12 (W. Europe Standard Time, UTC+01:00)

I have been tracking your project for a while, and it is very interesting!


Do you have an email address I can mail you directly to?


Miguel

Miguel de Icaza
Saturday, 02 November 2002 10:10:04 (W. Europe Standard Time, UTC+01:00)

Oh, we are tracking this bug, could you provide your thougths about why the .NET version is correct here:


http://bugzilla.ximian.com/show_bug.cgi?id=33183

Miguel de Icaza
Saturday, 02 November 2002 10:15:34 (W. Europe Standard Time, UTC+01:00)

Actually, can you tell us what the output of this program is with .NET 1.1:


using System;
class X {
static void Main ()
{
object o = new Y ();
Console.WriteLine(o.GetHashCode());
Console.WriteLine(typeof(object).GetMethod("GetHashCode").Invoke(o, null));
Console.WriteLine(typeof(Y).GetMethod("GetHashCode").Invoke(o, null));
}
}


class Y {
public override int GetHashCode ()
{
return 1;
}

}

Miguel de Icaza
Sunday, 03 November 2002 09:26:13 (W. Europe Standard Time, UTC+01:00)

This has now been fixed on Mono CVS. Check out the url above for more details
on Invoke.b
Miguel de Icaza
Sunday, 03 November 2002 09:57:11 (W. Europe Standard Time, UTC+01:00)

Thanks!
Name
E-mail
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Answer:  
Comment (HTML not allowed)  

Live Comment Preview