so I have read in the article that ==
check if the object reference is same or not .equals()
check if data is the same or not but when I am writing some program on my own I am having confusion.
I have a class person I which I am passing 10,20 in my constructor now I have created another object p1 and p2
person p = new person(10,20);
person p1;
p1 = p;
person p2=new person(10,20);
Console.WriteLine(p==p1); //true
Console.WriteLine(p.Equals(p1)); //true
Console.WriteLine(object.ReferenceEquals(p,p1)); //true
Console.WriteLine(p == p2); //false
Console.WriteLine(p.Equals(p2)); //false confusion same data
Console.WriteLine(object.ReferenceEquals(p, p2));//false
now I have confusion in p.equals(p2) both have the same data 10,20 so why false
p.Equals(p1)
it will use Equals method of your object.You did not implement it will compare address of two object, if it same address in memory it will true.
Note
p1 = p
that p1 and p refer to same address in memory.person p2=new person(10,20)
it will create object in difference address in memory with same content ofp
objectp.Equals(p2) has difference address in memory and no implement Equals method, it will return false.
You can implement Equals method like this
And you need implement GetHashCode method too like