# 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]