0

Below is the sample code that i am trying out:

namespace ConsoleApplication9
{
    class Program
    {
        public class MyBaseClass
        {
            public string name = "";
        }
        static void Main(string[] args)
        {
            MyBaseClass mybase = new MyBaseClass();
            mybase.name = "n";
            MyBaseClass mybase2 =  new MyBaseClass();
            mybase2.name = "n";
 Console.WriteLine("comparison using == {0}", mybase == mybase2); // prints false, which i understand both object's references are not same 
            Console.WriteLine("Comparions using Equasl {0}", mybase.Equals(mybase2)); // prints False again , Why ??
Console.ReadKey();
        }
    }
}

Now my understanding is that == operator compares the reference of two objects while .Equals checks if the content is same or not. If .Equals checks that the content is same or not, why mybase.Equals(mybase2) prints False for me. Both objects mybase and mybase2 have the same content.

Edit1:

Question1: Based on some of the responses and comments that by default .Equals checks for references unless overridden, I would like to understand why "==" and ".Equals" prints different results for following code:

   object string1 = new string(new char[] { 't', 'e', 's', 't' });
object string2 = new string(new char[] { 't', 'e', 's', 't' });
Console.WriteLine(string1==string2); // prints false
Console.WriteLine(string1.Equals(string2));  // prints true

Here both string1 and string2 are independently created, as such my understanding is since "==" checks for references it is printing false, but why ".Equals" prints true if ".Equals" default behavior is that it also checks for references??

Question2: Also for the following code :

string a = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
string b = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });

Console.WriteLine(a == b); //prints true
Console.WriteLine(a.Equals(b)); // prints true

why both "==" and ".Equals" prints true. Are these two strings not completely different in C# ??

Rajeev Verma
  • 81
  • 1
  • 8
  • `Equals` will compare what it is implemented to compare. In your case you don not implement it at all, making it falling back on `RefereneEquals`. – MakePeaceGreatAgain Oct 21 '18 at 15:03
  • You have to implement your comparaison yourself to take into account the name, otherwise it'll just compare the references. – Haytam Oct 21 '18 at 15:04
  • Hmm, how looks like a duplicate of [C# difference between == and Equals()](https://stackoverflow.com/q/814878) and maybe also [When would == be overridden in a different way to .equals?](https://stackoverflow.com/q/48120905). – dbc Oct 21 '18 at 17:30

1 Answers1

0

MyBase.Equals has to be overriden. By default Object.Equals compares references

EDIT Here is an implementation example of MyBase.Equals that will give true when calling .Equals on instances of MyBaseClass:

public class MyBaseClass : IEquatable<MyBaseClass>
{
  public string name = "";
  public bool Equals(MyBaseClass other)
  {
    return other != null &&
           name == other.name;
  }
}
Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
  • Thanks @ Emmanuel DURIN. I still don't completely understand, if by default it compares references, then why it prints true for below code, Isn't the references of str and str1 different. == prints false here. I have no additional code in my application apart from below: object str = new string(new char[] { 't', 'e', 's', 't' }); object str1 = new string(new char[] { 't', 'e', 's', 't' }); Console.WriteLine(str==str1); // false Console.WriteLine(str.Equals(str1)); // true so in this case why == prints false, but .equals prints true. – Rajeev Verma Oct 21 '18 at 16:11
  • Because `string.Equals` is implemented; it overrides `object.Equals`. – Caramiriel Oct 21 '18 at 16:51