)之间的``==和.Equals C#差( [英] C# difference between `==` and .Equals()

查看:131
本文介绍了)之间的``==和.Equals C#差(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Silverlight应用程序,用于比较两个字符串,因为某些原因,当我使用条件 == 返回,而 .Equals()返回真正。这里是code:

I have a condition in a silverlight application that compares 2 strings, for some reason when I use == it returns false while .Equals() returns true. Here is the code:

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

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

任何理由,为什么这是怎么回事?

Any reason as to why this is happening?

推荐答案

== 用在类型的前pression 对象,它会解析 System.Object.ReferenceEquals

When == is used on an expression of type object, it'll resolve to System.Object.ReferenceEquals.

等于只是一个虚拟方法和行为正因为如此,所以重写版本将使用(其中,为字符串类型比较的内容)。

Equals is just a virtual method and behaves as such, so the overridden version will be used (which, for string type compares the contents).

这篇关于)之间的``==和.Equals C#差(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆