Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

string - Difference between == operator and Equals() method in C#?

What is the difference between == and Equals() with example? I know that == is used to compare operator and Equals() method is used to compare content of string.So i tried

// first example
string s1 = "a";
string s2 = "a";
Console.Write(a.Equals(s2)); // returns true, but if I assign "b" to s2,
                             // then result will be false

// second example
string s1 ="a";
string s2 ="a";
Console.Write(s1 == s2);     // returns true

How this is so? Both are different object references. Suppose we consider that these are reference. But I tried to use like this

string s1 = new string("ab");
string s2 = new string("ab");

I am getting compile time error that can not convert string to char

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several things going on. Firstly, in this example:

string s1 = "a";
string s2 = "a";
Console.WriteLine(s1 == s2);

You claim that:

Both are different object reference.

That's not true due to string interning. s1 and s2 are references to the same object. The C# specification guarantees that - from section 2.4.4.5 of the C# 4 specification:

When two or more string literals that are equivalent according to the string equality operator (§7.10.7) appear in the same program, these string literals refer to the same string instance.

So in this particular case, you would still get "true" even if you printed object.ReferenceEquals(s1, s2), or if you made it use a true reference identity comparison with ==:

object s1 = "a";
object s2 = "a";
Console.WriteLine(s1 == s2); // Still prints True due to string literal interning

However, even if these were references to separate objects, == is overloaded for string. Overloading is a compile-time decision - the implementation to use depends on the compile-time types of the operands. So for example:

string a = new string('x', 1);
string b = new string('x', 1);
Console.WriteLine(a == b); // Uses string's implementation, prints True

object c = a;
object d = b;
Console.WriteLine(c == d); // Reference identity comparison, prints False

Compare that with object.Equals(object) which is a virtual method. As it happens, String overloads this method as well, but importantly it overrides it. So if we change our code to:

string a = new string('x', 1);
string b = new string('x', 1);
Console.WriteLine(a.Equals((object) b));

object c = a;
object d = b;
Console.WriteLine(c.Equals(d));

... then both method calls in the compiled code will simply be to object.Equals(object), but they'll still both print True because of polymorphism: the implementation in String will be used.

Here's what a call to the overloaded method would look like:

string a = new string('x', 1);
string b = new string('x', 1);
Console.WriteLine(a.Equals(b)); // Calls string.Equals(string)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...