开发者

String interning and equality operators

I was just reading this site which states: Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references...a and b do not refer to the same string instance ( http://msdn.microsoft.com/en-us/library/362314fe.aspx ).

I haven't checked the internals of the String class, but is that stateme开发者_如何学编程nt correct? From what I understand, the reason a String is immutable is due to string interning. In other words, only one copy of a string is stored for each unique value. All String variables with identical values reference the same object. I thought that was why "a" == "a" works -- not because it is defined to compare the values. If it were checking the value, then the strings would have to be compared character by character, causing significant performance considerations, and eliminating one of the main reasons to use string interning to begin with.

Maybe they over-simplified, but I think it's misleading to suggest that the String equality operators have been defined differently than other reference types. Please do correct me if I'm wrong!


String can be interned, but they don't have to be. String literals are interned (by default - this can be changed with the CompilationRelaxations.NoStringInterning attribute now), and instances created at runtime could be, but in general aren't unless special steps are taken (like calling String.Intern()).

There can be multiple instances of stings that have the same value.

Also, there are reasons besides being able to intern strings that they're immutable - immutability is mainly so objects holding references don't have to worry about those values changing 'behind their backs'. So it's more along the lines of being able to inter strings is a consequence of immutability, rather than strings must be immutable so we can intern them.


Not all strings are interned; literals / constants from the compiler (and specifically, the ldstr IL code) are interned automatically, and you can intern/check manually - but most strings constructed at runtime (for example from user-input, loaded from files/databases, or concatenation with values) are not. So yes: it is necessary to do value checks too.

Interning is an optimisation only; it needs to work without it.


No, the docs say exactly what they mean: for Strings, the == operators compares the values of the strings, character by character. Just because a given is a reference type doesn't mean it has to compare by reference; that's why operator== can be overloaded.

A bit of poking with Reflector reveals that String's operator==(String, String) calls Equals(String, String), which uses unsafe pointers to compare the underlying values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜