在C#==操作符和equals()方法之间的区别? [英] Difference between == operator and Equals() method in C#?
问题描述
什么是之间的==
和等于()
以实例的区别?我知道, ==
用于比较运营商和等于()
方法被用来比较string.So内容我试图
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");
我越来越不能字符串转换成char编译时错误
I am getting compile time error that can not convert string to char
推荐答案
有几件事情怎么回事。首先,在本实施例
There are several things going on. Firstly, in this example:
string s1 = "a";
string s2 = "a";
Console.WriteLine(s1 == s2);
您声称:
两者是不同的对象引用。
Both are different object reference.
这是不正确的,由于的字符串实习的。 S1
和 S2
都是同一个对象的引用。 C#的规范保证 - 从C#4规范的第2.4.4.5:
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:
当两个或多个字符串,根据字符串相等运算符(§7.10.7)出现在同一个程序是相同的,这些字符串引用相同的字符串实例。
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.
因此,在这种特殊情况下,你仍然可以得到真实,即使你打印 object.ReferenceEquals(S1,S2)
,或者如果你使它使用真正的与 ==参考身份比较
:
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
相比之下,与的Object.Equals(对象)
这是一个虚拟的方法。碰巧的是,字符串
重载这种方法的以及的,但重要的是它会覆盖它。因此,如果我们改变我们的code为:
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));
...那么这两个方法在编译code调用只会是的Object.Equals(对象)
,但他们会的还是的两个打印正确的,因为多态:在字符串的实施
将用于
... 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)
这篇关于在C#==操作符和equals()方法之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!