-2
class FirstClass { }
class SecondClass { }
class Program
{
    private static void Main(string[] args)
    {
       var firstClass1  = new FirstClass();
       var firstClass2  = firstClass1;
       var secondClass1 = new SecondClass();;
       var secondClass2 = secondClass1;

       object null1 = null;
       object null2 = null;

       int a = 10, b = 10, c = 20;

       Console.WriteLine("firstClass1 == firstClass2:\t"   + SameReference(firstClass1, firstClass2));
       Console.WriteLine("secondClass1 == secondClass2:\t" + SameReference(secondClass1, secondClass2));
       Console.WriteLine("firstClass1 == secondClass1:\t"  + SameReference(firstClass1, secondClass1));
       //Console.WriteLine("null1 == null2:\t" + SameReference(null1, null2));
       Console.WriteLine("null1==firstClass1:\t" + SameReference(null1, firstClass1));
       Console.WriteLine("a == b:\t" + SameReference(a, b));
       Console.ReadKey();             
    }
    public static bool SameReference(object object1,object object2)
    {
        if ((object1 == null && object2 != null) || (object1 != null && object2 == null))
            return false;
        if ((object1 == null && object2 == null) || (object1.GetHashCode() == object2.GetHashCode()))
        {
            Console.WriteLine(object1.GetHashCode() + "\t" + object2.GetHashCode());
            return true;
        }
        return false;
    }
}

In the above code, GetHashCode() method is returning 10 and 10 for a and b, but I want to compare addresses. That is how GetHashCode() method should work.please explain.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42

3 Answers3

4

Please see the docs (emphasis mine):

The GetHashCode method can be overridden by a derived type. If GetHashCode is not overridden, hash codes for reference types are computed by calling the Object.GetHashCode method of the base class, which computes a hash code based on an object's reference; for more information, see RuntimeHelpers.GetHashCode. In other words, two objects for which the ReferenceEquals method returns true have identical hash codes. If value types do not override GetHashCode, the ValueType.GetHashCode method of the base class uses reflection to compute the hash code based on the values of the type's fields. In other words, value types whose fields have equal values have equal hash codes. For more information about overriding GetHashCode, see the "Notes to Inheritors" section.

According to this answer, GetHashCode() for int is intended to return the value of the int. This can also be seen if you look at the C# source code for Int32:

// The absolute value of the int contained.
public override int GetHashCode() {
    return m_value;
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
2

GetHashCode() is working as expected. As per MSDN:

A hash code is a numeric value that is used to insert and identify an object in a hash-based collection such as the Dictionary class, the Hashtable class, or a type derived from the DictionaryBase class. The GetHashCode method provides this hash code for algorithms that need quick checks of object equality.

As far as GetHashCode() is concerned, the hash code for int a=10,b=10 would always be the same, as - because an int is a primitive type - a and b are both equal to each other.

Hoppeduppeanut
  • 1,109
  • 6
  • 20
  • 29
1

if you want to check reference equality (that is helpful when you have object from same class not object from two different classes) then you should use

.ReferenceEquals() tests whether or not two objects are the same instance and cannot be overridden.

Because for GethashCode :-

Two objects that are equal return hash codes that are equal. 
However, the reverse is not true:

For this you should override GetHashcode in your class as below and calculate hashcode based on property and then try to find equal or not by overriding Equal method.

public class FirstClass 
{
    public int Id {get;set;}
    public override bool Equals(Object obj)
    {
       if (!(obj is FirstClass)) return false;

       FirstClass f = (FirstClass) obj;
       return thid.Id == f.id;
    }

    public override int GetHashCode()
    { 
        return this.Id;
    } 
}

Just from you code you have objects of two different classes as they are belong to two different class they are not going to be equal. Please refer C# documentation for GetHashCode and Equal.

so this line of code is invalid :

Console.WriteLine("firstClass1==secondClass1:\t"+SameReference(firstClass1,secondClass1));
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263