equals vs == c#

Hi Programmers,Here is the article to compare == Operator and the Equals() method in C#.Net. == Operator is used to compare value type data items and the Equals() method is used reference type data items. The Equality Operator ( ==) is the comparison operator and the Equals() method compares the contents of a string. The == Operator compares the reference identity while the Equals() method compares only contents.
Let’s see with some examples.

1.ob1 = p1 that means, p1 value goes to ob1 so == operator return true and equals method return also true because reference and contents are same but p2 == ob2. == Operator returns False because it compares the reference identity while the Equals() method returns True because it compares the contents of the objects. see below image.

2.when two strings has null value then == operator return true
Equal method return false. see below image.

3.== Operator returns true because it compares the reference identity while the Equals() method returns True because it compares the contents of the objects. see below image.

Happy Programming …Thanks.

Post Author: adama