Trying to understand == operator with objects
1 2 3 4 5 6 7 | object a ="1"; object b ="1"; Console.WriteLine(a == b); // returns True object c = 1; object d = 1; Console.WriteLine(c == d); // returns False |
上面的代码返回整数和字符串的不同结果。我不明白为什么。有人能帮我理解这背后的原因吗?
那么,EDOCX1(operator)和EDOCX1(function)之间的区别是什么?
将整数声明为
edit:per@enigmativity,我错过了两个字符串声明为
虽然Ed S已经回答了
For predefined value types, the equality operator (==) returns true if
the values of its operands are equal, false otherwise. For reference
types other than string, == returns true if its two operands refer to
the same object. For the string type, == compares the values of the
strings.
如果要比较对象,则可以使用
同样,当你问==和referenceequals之间的区别时,你应该注意到
所以如果你这么说
1 2 3 4 | string x ="ABCD"; string y = 'A' +"BCD"; // ensure it's a different reference if (x == y) { // evaluates to TRUE |
因为用于比较变量x和y的方法是在编译时确定的。字符串是不可变的,因此使用
然而,当使用
1 2 3 4 | object x ="ABCD"; object y = 'A' +"BCD"; // ensure it's a different reference if (x == y) { // evaluates to FALSE |
反之
1 2 3 4 | object x ="ABCD"; object y = 'A' +"BCD"; // ensure it's a different reference if (x.Equals(y)) { // evaluates to TRUE |
此外,您还可以检查重写equals()和operator==(C编程指南)的指导原则。
In C#, there are two different kinds of equality: reference equality
(also known as identity) and value equality. Value equality is the
generally understood meaning of equality: it means that two objects
contain the same values. For example, two integers with the value of 2
have value equality. Reference equality means that there are not two
objects to compare. Instead, there are two object references and both
of them refer to the same object.[...]
By default, the operator == tests for reference equality by
determining whether two references indicate the same object.
Therefore, reference types do not have to implement operator == in
order to gain this functionality. When a type is immutable, that is,
the data that is contained in the instance cannot be changed,
overloading operator == to compare value equality instead of reference
equality can be useful because, as immutable objects, they can be
considered the same as long as they have the same value. It is not a
good idea to override operator == in non-immutable types.
此代码返回
1 2 3 4 | object a ="a"; object b = a +"b"; a ="ab"; Console.WriteLine(a == b); // returns False |
此代码返回
1 2 3 | object a ="ab"; object b ="ab"; Console.WriteLine(a == b); // returns True |
但在这两种情况下,
因此,字符串作为对象正在将
c中的
示例中字符串比较相等的原因不是生成的代码正在检查其内容,而是与实现字符串文本的方式有关。编译程序集时,编译器将生成一个包含所有出现在其中的字符串文本的列表;该列表中所有字符串的长度和内容都作为一个blob包含在生成的程序集中。在代码中的每个位置使用字符串文字,编译器插入一条指令"加载对第*n*个字符串的引用",在生成所有代码后,编译器在程序集中包含其中包含的每个字符串文字的字符序列。加载程序集时,.NET运行时将创建一个包含
如果编译器和/或运行时碰巧注意到同一个字符序列出现在多个字符串文字中,则通过引用为较早的字符串生成的
在您的示例中,文字
定义如下:来自String.cs
1 2 | public static bool operator == (String a, String b) { return String.Equals(a, b); |
等方法的定义是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static bool Equals(String a, String b) { if ((Object)a==(Object)b) { return true; } if ((Object)a==null || (Object)b==null) { return false; } if (a.Length != b.Length) return false; return EqualsHelper(a, b); } |