Coder Perfect

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


In a Silverlight application, I have a condition that compares two strings; however, when I use ==, it returns false, whereas. The equals() function returns true.

The code is as follows:

if (((ListBoxItem)lstBaseMenu.SelectedItem).Content.Equals("Energy Attack"))
    // Execute code

if (((ListBoxItem)lstBaseMenu.SelectedItem).Content == "Energy Attack")
    // Execute code

Is there any explanation for why this is happening?

Asked by Drahcir

Solution #1

When you use == on an object expression, it resolves to System. Object.ReferenceEquals.

Because Equals is a virtual method that behaves like one, the overridden version will be used (which, for string type compares the contents).

Answered by mmx

Solution #2

The particular behavior of the == operator pertaining to the string class is ignored when comparing an object reference to a string (even if the object reference relates to a string).

Equals compares values in most cases (except when dealing with strings), whereas == compares object references. If two objects you are comparing are referring to the same exact instance of an object, then both will return true, but if one has the same content and came from a different source (is a separate instance with the same data), only Equals will return true. However, as noted in the comments, string is a special case because it overrides the == operator so that when dealing purely with string references (and not object references), only the values are compared even if they are separate instances. The following code illustrates the subtle differences in behaviors:

string s1 = "test";
string s2 = "test";
string s3 = "test1".Substring(0, 4);
object s4 = s3;

Console.WriteLine($"{object.ReferenceEquals(s1, s2)} {s1 == s2} {s1.Equals(s2)}");
Console.WriteLine($"{object.ReferenceEquals(s1, s3)} {s1 == s3} {s1.Equals(s3)}");
Console.WriteLine($"{object.ReferenceEquals(s1, s4)} {s1 == s4} {s1.Equals(s4)}");

The output is:

True True True
False True True
False False True

Answered by BlueMonkMN

Solution #3

== as well as The behavior stated in the actual type and the real type at the call point are both reliant on equals. Both are simply methods / operators that may be applied to any type and given any behavior the programmer wishes. It is usual for folks to implement in my experience. On an object, equals is used, but the operation == is not used. This implies that. Equals will determine whether the values are equal, whereas == will determine whether they are the same reference.

When I’m working with a new type whose definition is in flux or writing generic algorithms, I find the best practice is the following

When I believe the use of == is confusing, I shall use Object explicitly. To eliminate uncertainty in the code, use reference equals.

Eric Lippert just published a blog post explaining why the CLR has two approaches of equality. It’s well worth your time to read.

Answered by JaredPar

Solution #4

== Operator


Answered by kashif

Solution #5

As far as I can see, the solution is straightforward:

I hope I’m correct and that my response has answered your question.

Answered by Liraz Shaka Amir

Post is based on