2
 class A
    {
     public int i;
        public A()
        {
            i = 10;
        }
     }

  class Program
    {
        static void Main(string[] args)
        {


    A a = new A();  // creating class object
    A b = new A();// creating class object

    if (a.Equals(b)) // checking whether they are equal not by ==
    {
        Console.WriteLine("Both objects are equal ");
    }
    else
    {
        Console.WriteLine("Both objects are not equal ");
    }
    Console.ReadLine();
}

// O/P : Both objects are not equal
        }
    }

I have declared two object of class A. Both are initialized with data member value i = 10 in the constructor. But on comparing the objects, the output is "Both objects are not equal".

I would like to know why the objects are not equal.

Bonifacio2
  • 3,405
  • 6
  • 34
  • 54
mahi batra
  • 21
  • 1
  • 2
  • Their respective references are not the same. From documentation: `If the current instance is a reference type, the Equals(Object) method tests for reference equality, and a call to the Equals(Object) method is equivalent to a call to the ReferenceEquals method. Reference equality means that the object variables that are compared refer to the same object.` – Sani Huttunen Apr 24 '18 at 09:24
  • if you want to compare the values you would have to do a.i == b.i. However, I think you are comparing memory references Using Class.Equals. – João Silva Apr 24 '18 at 09:25
  • Possible duplicate of [How do I check if an object is equal to a new object of the same class?](https://stackoverflow.com/questions/20701507/how-do-i-check-if-an-object-is-equal-to-a-new-object-of-the-same-class) – Nico Haase Apr 24 '18 at 09:30

2 Answers2

2

As per my knowledge "Equal" for reference type object compare reference content.

No. The default Equals implementation only compares the references, not the content in those references. And a and b in your example are two different instances of A, so two different references.

To make your Equals work you need to override Equals (and GetHashCode so that it returns equal hashes for instances where Equals will return true and ideally different hashes for non-equal instances):

class A
{
     public int i;
     public A()
     {
         i = 10;
     }
     public override bool Equals(object o)
     {
         return (o as A)?.i == this.i;
     }
     public override int GetHashCode()
     {
         return i.GetHashCode();
     }
 }
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • @mahibatra yes, `StringBuilder` has [it's own `Equals` overload](https://referencesource.microsoft.com/#mscorlib/system/text/stringbuilder.cs,d667f7b9914a12ec) – René Vogt Apr 24 '18 at 09:35
  • But if i use stringbuilder object e.g StringBuilder s1 = new StringBuilder('H', 'I'); StringBuilder s2 = new StringBuilder('H', 'I'); Then s1.Equal(s2) return true. Here references are different for s1 and s2 Then why s1.Equal(s2) return true. – mahi batra Apr 24 '18 at 09:37
  • @mahibatra see above comment. – René Vogt Apr 24 '18 at 09:37
0

The Equals() method tests if two refrences refer to the same instance of a class, specifically if the two references contain the same adress in memory. In your case a.Equals(b) returns false because you created two different instances of the class A so they don't contain the same adress in memory. For example a.Equals(b) would return true in the following case:

  A a = new A();
  A b = a;

Because a and b are now pointing to the same object in the memory (they share the same adress in memory).

Slaven Tojić
  • 2,945
  • 2
  • 14
  • 33