简体   繁体   English

.NET编译器如何比较两个字符串?

[英]How does .NET compiler compare two strings?

string a="I am comparing 2 string";
string b="I am comparing 2 string";

if(a==b)
  return true;
else
  return false;

How does a .NET compiler compare two strings? .NET编译器如何比较两个字符串? Does a string work like a struct(int)? 字符串是否像struct(int)一样工作? string is class so a=b means we are comparing 2 object, but i want to compare 2 values. string是class所以a = b意味着我们正在比较2个对象,但我想比较2个值。

The String class overloads the == operator, so yes it compares the values of the strings, just like comparing value types like int . String类重载了==运算符,所以它是比较字符串的值,就像比较像int这样的值类型。

(On a side note, the compiler also interns literal strings in the code, so the string variables a and b will actually be referencing the same string object. If you use Object.ReferenceEquals(a,b) it will also return true .) (另一方面,编译器还会在代码中实现文字字符串,因此字符串变量ab实际上将引用相同的字符串对象。如果使用Object.ReferenceEquals(a,b)它也将返回true 。)

System.String is a class which has the == operator overloaded to compare the content of the strings. System.String是一个重载了==运算符的类,用于比较字符串的内容。 This allows it to be "value like" in comparison and yet still be a reference type in other respects. 这允许它在比较中是“有价值的”,但在其他方面仍然是参考类型。

Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references. 虽然string是引用类型,但是定义了相等运算符(==和!=)来比较字符串对象的值,而不是引用。 This makes testing for string equality more intuitive. 这使得对字符串相等性的测试更加直观。

C# string C#字符串

Strings are compared by the Runtime, not the compiler. 字符串由运行时进行比较,而不是编译器。 Comparison is performed by Equality operator. 比较由Equality运算符执行。

There are different things to keep in mind here. 这里要记住不同的事情。

First, all identical constant strings will be interned so that both references are equal to start with. 首先,所有相同的常量字符串将被实现,以便两个引用都等于start。 Therefore, even if you did a ReferenceEquals() here, you'd get "true" as result. 因此,即使你在这里做了一个ReferenceEquals() ,你也会得到“真”的结果。 So only for a string built (for instance with a StringBuilder , or read from a file etc.) you'd get another reference and therefore false when doing a reference equality comparison. 因此,仅对于构建的字符串(例如使用StringBuilder ,或从文件中读取等),您将获得另一个引用,因此在进行引用相等性比较时会出现错误。

If both objects are known to be strings at compile time, the compiler will emit code to compare their value ( == overloaded operator on System.String ), not their references. 如果在编译时已知两个对象都是字符串,则编译器将发出代码以比较它们的值(在System.String上的==重载运算符),而不是它们的引用。 Note that as soon as you compare it against an object type reference, this isn't the case anymore. 请注意,只要将其与object类型引用进行比较,就不再是这种情况了。

No runtime check is done to compare a string by value, and the compiler does not emit a .Equals() call for the == operator. 没有进行运行时检查以按值比较字符串,并且编译器不会为==运算符发出.Equals()调用。

Notice that your question is a little tricky. 请注意,您的问题有点棘手。 Because ReferenceEquals will ALSO return true. 因为ReferenceEquals也会返回true。

This is because of Interning : http://en.wikipedia.org/wiki/String_interning 这是因为Interning: http//en.wikipedia.org/wiki/String_interning

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM