• Feeds

    Subscribe in a reader

  • Ads

Fun with string interning

As several people correctly pointed out, my recent issues with string comparison were due to string interning.

String interning is a (usually) transparent process where by the CLR tries to guarantee that two strings that are value-equivalent are also reference-equivalent. Usually, but not always. The interning rules are quite complex, and I don’t claim to understand them all.

I didn’t write the code that was doing the comparison, so I didn’t have the ability to turn the reference equality comparison into a value comparison (String.Equals()). The only thing I could do was change my code to return String.Empty instead of the compile-time constant “”.

Guess I’ll be more dilligent about returning String.Empty in the future.

 

#1 TSHAK on 8.01.2005 at 11:26 AM

"Guess I’ll be more dilligent about returning String.Empty in the future."You see kids, this is what happens when you ignore your static analysis warnings. :-)

#2 hsolive on 8.16.2005 at 5:58 AM

You're missing the point. The interning is a space opitimization by the compiler. You should never rely on it in any way unless you're doing your own interns.

#3 alize on 9.10.2005 at 1:00 PM

"Guess I’ll be more dilligent about returning String.Empty in the future."

#4 ada on 9.16.2005 at 11:11 AM

"Guess I’ll be more dilligent about returning String.Empty in the future." You see kids, this is what happens when you ignore your static analysis warnings. :-)