为什么要使用String.Equals了==? [英] Why would you use String.Equals over ==?

查看:172
本文介绍了为什么要使用String.Equals了==?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我被介绍给一个大codeBase的,发现所有的字符串比较使用做了 String.Equals()而不是 ==

I recently was introduced to a large codebase and noticed all string comparisons are done using String.Equals() instead of ==

什么是这种情况的原因,你觉得呢?

What's the reason for this, do you think?

推荐答案

这是完全可能的开发人员群体的很大一部分,其中使用来自一个Java背景 == 比较字符串是错误的,是行不通的。

It's entirely likely that a large portion of the developer base comes from a Java background where using == to compare strings is wrong and doesn't work.

在C#有没有(实用)差(字符串),只要他们是类型为字符串。

In C# there's no (practical) difference (for strings) as long as they are typed as string.

如果它们被类型为对象 T 然后在这里看到其他的答案是谈论泛型方法或运算符重载因为你一定要使用Equals方法。

If they are typed as object or T then see other answers here that talk about generic methods or operator overloading as there you definitely want to use the Equals method.

这篇关于为什么要使用String.Equals了==?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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