• Feeds

    Subscribe in a reader

  • Ads

Fun with strings

I lost a good hour to this last night…too much time staring at the debugger output and trying to convince myself that I wasn’t completely crazy.

Say you have the following code:

string str1 = "";string str2 = "";Debug.Assert( (object)str1 == (object)str2 );

Under what conditions will this assert fail?

#1 Miki Watts on 7.29.2005 at 10:18 AM

Just guessing, but if one string hasn't been interned yet and the other has?

#2 Saurabh Nandu on 7.29.2005 at 10:39 AM

Just guessing == on objects checks if they refer to the same reference - rather than they have the same value. like Miki brought out in case the other string is not interned. I am also wondering ..it fails always? just because while casting to objects causes it to create two *different* object references?

#3 Scott Allen on 7.29.2005 at 10:46 AM

I'm at a loss on this one - both str1 and str2 should reference a string in the intern pool. That would happen on assembly load according to Brumme's blog. Do you mean to say when would two empty string not reference the same instance? Whenever an string is created on the heap.string s1 = "";string s2 = String.Empty;string s3 = new string('\x0', 0);Debug.Assert(s1 == s2);Debug.Assert(s1 == s3);Debug.Assert((object)s1==(object)s2);Debug.Assert((object)s1==(object)s3); // boom!!

#4 Scott Allen on 7.29.2005 at 10:48 AM

To clarify - the interning of string literals takes place on assembly load according to CB's blog, so you should never hit code where one literal is interned and the other is not.

#5 Patrick Cauldwell on 7.29.2005 at 12:34 PM

Might that then imply that empty strings are treated differently?Is the empty string interned?

#6 haacked@gmail.com (Haacked) on 7.30.2005 at 12:08 AM

Now, it's not clear from the snippet, but suppose string 1 and 2 are member variables and not locals.Another thread could swoop in there and change str1 or str2 after the initial assignment, but before the debug assert.Probably not the answer you're looking for, but others already mentioned the interning possibility.

#7 Wesner Moise on 7.30.2005 at 11:50 AM

The conditions were relaxed in Whidbey, perhaps because interning did not provide the promise benefits of saving memory.I am guessing that the same string from two different assemblies may produce different references.

#8 lele on 8.06.2005 at 10:56 AM

string s1 = ""; string s2 = String.Empty; string s3 = new string('\x0', 0); Debug.Assert(s1 == s2); Debug.Assert(s1 == s3); Debug.Assert((object)s1==(object)s2); Debug.Assert((object)s1==(object)s3); // boom!!

#9 hsolive on 8.16.2005 at 5:59 AM

To clarify - the interning of string literals takes place on assembly load according to CB's blog, so you should never hit code where one literal is interned and the other is not.

#10 ada on 9.16.2005 at 11:12 AM

Depend on the optimizer. If .NET compiler is smart enough, the two empty strings are actually one and the assert will not fail. Otherwise two different objects should not equal.