Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was Equals() and == designed to be different?

Tags:

c#

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
like image 550
Thomas Eyde Avatar asked May 23 '15 12:05

Thomas Eyde


People also ask

What is the difference between == and Equals () method?

== 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.

Why is == Equals not?

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 .

What is the difference between == operator and Equals () in String wrapper classes?

== is a reference comparison, i.e. both objects point to the same memory location. . equals() evaluates to the comparison of values in the objects.

What is the difference between Equals () and == C#?

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.


1 Answers

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.

like image 92
David Arno Avatar answered Oct 20 '22 16:10

David Arno