默认情况下, == 运算符对字符串如何工作?

发布于 2024-11-01 10:01:49 字数 1080 浏览 0 评论 0原文

可能的重复:
请告诉为什么在字符串的情况下,字符串对象的两个引用是相同的(下面编写的代码)

当我在 C# 中编写 s1 == s2 时,两者都声明为字符串,编译器会比较引用或内容吗?即如果 ==string 类覆盖?

我只是想知道为什么这段代码打印“true”:

string s1 = "hello"
string s2 = s1 + " ";
s2 = s2.Trim(); // i expect new object here
Console.WriteLine(s2 == s1);

此外,我还有一些第三方来源,其中 == 经常用于字符串比较。这让我抓狂,因为我几乎从不使用 == 来比较 Java 中的字符串,现在我无法理解这段代码是如何工作的。

C#中使用==比较字符串正常吗?

upd:感谢大家,几乎所有答案都是正确的。 结论:

  • 是的,在C#中使用“==”来比较字符串是正常的,
  • 字符串将通过内容(而不是引用)进行比较
  • == 运算符对于字符串不是虚拟的。
  • C# 中的字符串是不可变的(这与 Java 类似)

此行为与 Java 不同,其中 String 类的“==”比较引用。

另请参阅 为什么字符串不比较引用?

在我个人看来,语言不应该允许覆盖或重载 == 运算符,因为它使它像 C++ 一样困难:)

Possible Duplicate:
Please tell why two references are same for string object in case of string( Code written below)

When I write in C# s1 == s2 where both declared as string would compiler compare references or content? I.e. if the == is overriden for string class?

I just wonder why this code prints "true":

string s1 = "hello"
string s2 = s1 + " ";
s2 = s2.Trim(); // i expect new object here
Console.WriteLine(s2 == s1);

Also I've some third-party sources where == is often used for strings comparision. This makes me crazy because I almost never use == to compare strings in Java and now I can't understand how this code works.

Is it normal to use == to compare strings in C#?

upd: Thanks to all, almost all answers are correct.
To conclude:

  • Yes, it's normal to use "==" to compare string in C#
  • strings will be compared by content (not reference)
  • == operator for strings is NOT virtual.
  • strings in C# are immutable (this is similar to Java)

This behavior is different to Java where "==" for String class compares references.

see also Why strings does not compare references?

In my personal opinion language should not allow override or overload == operator, cause it makes it as difficult as c++ :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

讽刺将军 2024-11-08 10:01:49

运算符不能被多态地重写,但可以被重载,字符串就是这种情况。重载检查内容是否相等(以顺序方式,没有文化敏感性)。因此,例如:

string s1 = "hello";
string s2 = (s1 + " ").Trim();

object o1 = s1;
object o2 = s2;

Console.WriteLine(s1 == s2); // True - calls overloaded ==(string, string)
Console.WriteLine(o1 == o2); // False - compares by reference

请注意这是如何在完全相同的对象上进行操作的,但是由于重载解析是在编译时执行的,因此在第二种情况下编译器不会执行重载解析。知道调用特定于字符串的运算符。

Operators can't be overridden polymorphically, but they can be overloaded which is the case for string. The overload checks for content equality (in an ordinal fashion, with no culture sensitivity). So, for example:

string s1 = "hello";
string s2 = (s1 + " ").Trim();

object o1 = s1;
object o2 = s2;

Console.WriteLine(s1 == s2); // True - calls overloaded ==(string, string)
Console.WriteLine(o1 == o2); // False - compares by reference

Note how this is operating on exactly the same objects, but because overload resolution is performed at compile-time, in the second case the compiler doesn't know to call the string-specific operator.

莫相离 2024-11-08 10:01:49

是的,默认情况下,当您使用 == 时,它会检查引用是否相等,但它会被字符串覆盖,以便它检查内容(因为字符串也是不可变的)

这是我喜欢的很棒的文章(作者乔恩也在这里:))

yes, by default when you use == it checks for reference equiality, but it is overriden for strings so that it checks for content (because strings are immutable as well)

This is great article that I like (and author Jon is here as well :) )

森林很绿却致人迷途 2024-11-08 10:01:49

即使 System.String 类是引用类型(“string”是其别名),该类型上的 == 操作方法也会被重写以提供字符串类型内容之间的比较。

它们在您的示例中是相等的,因为字符串类型的内容是相同的。

Even though the System.String class is a reference type (which 'string' is an alias to) the == operation method on the type is overriden to provide comparison between the contents of the string types.

They are equal in your example because the contents of the string types are the same.

沧桑㈠ 2024-11-08 10:01:49

s2 == s1 会给你一个布尔结果,是的。对于 .NET 编程,我总是发现使用 String.Compare 更好。

s2 == s1 will give you a boolean result, yes. As for .NET programming, I've always found it better to use String.Compare.

萌梦深 2024-11-08 10:01:49

是的,在 C# 中使用 == 比较字符串是正常的(但不一定是好的做法)。 String.Compare是一种更可靠的比较方式,它还可以照顾不同的字符集和区分大小写。

Yes, it's normal (but not necessarily good practice) to compare strings in C# with ==. String.Compare is a more reliable way of comparing, which can also take care of different character sets and case sensitivity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文