在C#中,==和Equals之间的区别

49 浏览
0 Comments

在C#中,==和Equals之间的区别

\n可能是重复问题:
\nC#中==和.Equals()的区别 \n

\n用于比较两个变量时,我们可以使用==或Equals方法。例如,\n

        string a = new string(new char[] {'a', 'b', 'c', 'd'});
        string b = new string(new char[] {'a', 'b', 'c', 'd'});
        Console.WriteLine (a==b);
        Console.WriteLine (a.Equals(b));

\n我的问题是何时应该使用==,何时应该使用Equals?这两者之间有什么区别吗?

0
0 Comments

== and Equals are two ways to compare objects in C#. However, they have different behaviors and use different methods for comparison.

When == is used on an object type, it resolves to System.Object.ReferenceEquals. This means it checks if the two objects being compared are the same instance or reference the same memory location. It does not compare the content or value of the objects.

On the other hand, Equals is a virtual method and behaves differently depending on the type of the object. When it is overridden for a specific type, the overridden version will be used. For example, for string type, Equals compares the contents or values of the strings.

In summary, == compares the references of the objects, while Equals compares the values or contents of the objects, depending on the type.

To clarify, when using ==, it does not actually "resolve" to ReferenceEquals. It simply loads the two references and performs a "ceq" or "beq" instruction, without calling the ReferenceEquals method.

If you want to compare the values or contents of two objects, you should use the Equals method. However, keep in mind that the behavior of Equals can vary depending on the type and how it is implemented.

Here is an example to demonstrate the difference between == and Equals:

string a = "hello";
string b = "hello";
string c = a;
Console.WriteLine(a == b); // True
Console.WriteLine(a.Equals(b)); // True
Console.WriteLine(a == c); // True
Console.WriteLine(a.Equals(c)); // True

In this example, the == operator compares the references of the strings a and b, and since they have the same value, it returns true. The Equals method, being overridden for string type, compares the contents of the strings and also returns true.

Similarly, when comparing a and c, both == and Equals return true because they refer to the same string instance.

In conclusion, understanding the difference between == and Equals in C# is crucial to ensure correct comparison of objects based on their references or values.

0
0 Comments

在C#中,有两个用于比较对象相等性的操作符,即"=="和Equals。然而,它们之间存在一些差异。这篇文章将解释为什么会出现这个问题,并给出解决方法。

当需要测试引用类型的相等性而不是引用标识时,应该使用Equals操作符。然而,对于字符串来说,使用"=="操作符会简化代码,使其更易读。但需要记住,为了使比较正常工作,操作符两边的表达式都必须是string类型。

对于值类型,一般我会使用"=="操作符,因为这样代码更易读。如果值类型提供了对"=="操作符的重载,并且其行为与Equals不同,那么就会变得复杂。但是,我认为这种类型的设计是非常糟糕的。

总之,对于大多数引用类型,应该使用Equals操作符来测试相等性。而对于字符串,使用"=="操作符会更简单明了。对于值类型,使用"=="操作符可以使代码更易读。然而,如果值类型重载了"=="操作符并且行为不同于Equals,那么需要谨慎使用。

更多详细信息可以参考原文:http://blogs.msdn.com/b/csharpfaq/archive/2004/03/29/when-should-i-use-and-when-should-i-use-equals.aspx

0
0 Comments

==和Equals在C#中的区别

在C#中,==是一个操作符,当没有被重载时,对于类来说代表“引用相等”,对于结构体来说代表字段相等,但是它可以被重载。它作为一个重载而不是一个覆盖的结果是它不具有多态性。

Equals是一个虚方法,这使得它具有多态性,但是需要小心不要在null实例上调用它。

根据以下规则:

- 如果你知道某个对象的类型,并且知道它有一个==重载(大多数核心类型如string,int,float等都有==的意义),那么应该使用==

- 如果你不知道类型,可能会推荐使用Equals(a,b),因为这样可以避免null的问题

- 如果你真的想检查是否为同一个实例(引用相等),考虑使用ReferenceEquals(a,b),因为即使==被重载和Equals被覆盖,这也能正常工作

- 在使用泛型时,考虑使用EqualityComparer.Default.Equals(a,b),它避免了null的问题,支持可空类型Nullable-of-T,支持IEquatable-of-T

如果==是引用比较,那么为什么两个基本类型之间的比较在值相等时返回true呢?比如这样:

~~~

a = 3 // 有自己的引用

b = 3 // 有自己的不同的引用

a == b // 返回true

~~~

为什么?这些引用是不同的,所以应该返回false,对吧?

我没有说“==是引用比较” - 我说的是当没有被重载时,对于类来说它代表引用相等,对于结构体来说它代表字段相等;“基本类型”是一个宽泛的术语 - 看起来你在谈论的是System.Int32(也叫int);int确实重载了==(包括在编译器级别),并且除非你进行装箱:那些根本不是引用 - 它们是值,因此询问这些引用是相等还是不相等是没有意义的。

原文地址:https://stackoverflow.com/questions/814693

0