Hi Guys
NP77 GetHashCode()
Following program when executed in the Microsoft Visual Studio .NET 2003 environment is giving Hash code: 2 as an output.
When executed in the Microsoft Visual 2005 Express Edition environment is giving Hash code: 58225482 as an output.
What is the reason for different output? Anyone knows please explain the reason.
Thank you
using System;
class ObjTest
{
public static int
{
// Make an instance of ObjTest.
ObjTest c1 = new ObjTest();
// Pump info to console.
Console.WriteLine("ToString: {0} ", c1.ToString());
Console.WriteLine("Hash code: {0} ", c1.GetHashCode());
Console.WriteLine("Base class: {0} ", c1.GetType().BaseType);
// Make some other references to c1.
ObjTest c2 = c1;
object o = c2;
// Are all 3 instances pointing to the same object in memory?
if (o.Equals(c1) && c2.Equals(o))
Console.WriteLine("Same instance!");
return 0;
}
}
Posted Feb 3, 2008, 8:56 AM
Thank you for your help, Alan.
AlanPosted Feb 3, 2008, 8:41 AM
This is due to differences in the default hashing algorithm used in different versions of the CLR.
For example, in Visual Studio.NET 2002 which uses CLR 1.0, the same code produces a value of 4.
However, in Visual Studio Express 2008 the value produced is the same as in Express 2005 as both versions use CLR version 2.0.
There is in fact a warning about this in the documentation:
http://technet.microsoft.com/en-us/library/system.object.gethashcode.aspx