This question is not about the differences between ==
and Equals
. It's about why they were designed different.
We know the differences causes many problems, which must have been pretty easy to spot up front:
Equals
and ==
yield different results for two instances of the same entity.==
yield different results when comparing subclasses, because ==
is not polymorphic== should be used during reference comparison. == checks if both references points to same location or not. equals() method should be used for content comparison. equals() method evaluates the content to check the equality.
The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .
== is a reference comparison, i.e. both objects point to the same memory location. . equals() evaluates to the comparison of values in the objects.
Difference between == and . Equals method in c# The Equality Operator ( ==) is the comparison operator and the Equals() method in C# is used to compare the content of a string. The Equals() method compares only content.
The short answer is that the C# language design team and the .NET framework design team couldn't agree on how best to compare values/objects for equality, so each implemented their own system.
For a more technical, detailed answer, please refer to a blog post on the subject by Eric Lippert.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With