<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Interning Strings and immutability</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx</link><description>Managed strings are subject to ‘interning’. This is the process where the system notices that the same string is used in several places, so it can fold all the references to the same unique instance. Interning happens two ways in the CLR. 1) It happens</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>RE: Interning Strings &amp; immutability</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#51372</link><pubDate>Wed, 23 Apr 2003 11:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:51372</guid><dc:creator>Oisin</dc:creator><description>Chris, again you make it appear easy to just babble on coherently about a difficult topic -- your words poke a finger in the eye of complexity.  If I tried to explain this to someone in as many words, it would be meaningless. Keep it up!

- Oisin

</description></item><item><title>RE: Interning Strings &amp; immutability</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#51373</link><pubDate>Wed, 23 Apr 2003 17:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:51373</guid><dc:creator>Chris Brumme</dc:creator><description>The CLR Interop team agreed with me: I do find the marshaling directives bewildering!  They pointed out that I should have mentioned that StringBuilder is the preferred way to call unmanaged methods that mutate strings.  (Be sure to set the capacity of the StringBuilder high enough to accept the text that will be placed in it).

The VBByRefStr technique is really there for VB compatibility reasons.</description></item><item><title>RE: Interning Strings &amp; immutability</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#51374</link><pubDate>Fri, 16 May 2003 04:17:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:51374</guid><dc:creator>Payo</dc:creator><description>First of all I&amp;#180;d like to congratulate you for your weblog, it&amp;#180;s really impressive.
And now the question: when you talk about mutating strings and say:
&amp;quot;Well, you can certainly use C#’s ‘unsafe’ feature or equivalent unverifiable ILASM or Managed C++ code to write into a string’s buffer.&amp;quot;
could you give me an example of this with C# and pointers?
I know how to do it with MSIL, but in C# I don&amp;#180;t know how to make a pointer point to the memory  address pointed by an object reference (a string in this case).
Maybe this is a stupid question, but I would really apreciate your answer.
Thanks a lot for sharing your knowledge with us.</description></item><item><title>RE: Interning Strings &amp; immutability</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#51375</link><pubDate>Mon, 19 May 2003 18:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:51375</guid><dc:creator>Chris Brumme</dc:creator><description>Payo,

With strings, the trick is the m_firstChar field, which gives you addressability over the string buffer.

Here's a lame example, that probably writes the first character of strIn throughout a returned string of the same size.  (I haven't tested it).  It shows how you can use C-style dereferencing via '*' or via array syntax.

       unsafe static string SlamFirst strIn) {
            int length = strIn.Length;
            String strOut = FastAllocateString(length);
            fixed (char * inBuff = &amp;amp;strIn.m_firstChar, outBuff = &amp;amp;strOut.m_firstChar) {
                char c = *inBuff;
                for(int i = 0; i &amp;lt; length; i++) {
                    outBuff[i] = c;
                }
            return strOut;
        }
        
With normal structs and classes, use explicit or sequential layout so that the C# compiler can predict what the CLR's loader will decide with respect to layout decisions.

When using constructs like this, be sure to mark your code as 'unsafe' and compile it with /unsafe.
</description></item><item><title>RE: Interning Strings &amp; immutability</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#51376</link><pubDate>Tue, 20 May 2003 02:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:51376</guid><dc:creator>Payo</dc:creator><description>Thanks a lot for your answer.
I&amp;#180;d just found one way to make it work:
// stupid sample code
fixed(char* s1p=s1)
		{
			for(int i=0;i&amp;lt;4;i++)
			{
				*(s1p+i)='t';
			}
		}
At first I was quite confused with all this stuff because I thought the fixed statement was something &amp;quot;optional&amp;quot; (if you were willing to have problems with the GC and object reallocation), and when I tried to compile the code above without the fixed statement the csc gave me this not too clear error:
&amp;quot;can not implicity convert type string to char* &amp;quot;
Using the fixed statement (so it looks it&amp;#180;s more or less compulsory) there&amp;#180;s no compiler error.

There was a small problem with the code you submitted, I guess you are one of the developers of the string class, so you are used to work with private fields and methods like m_firstChar and FastAllocateString, but I&amp;#180;m in the  &amp;quot;outer world&amp;quot;, so I don&amp;#180;t have access to those private fields-methods :-)
</description></item><item><title>Dangerous PInvokes - string modification</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#77745</link><pubDate>Sun, 22 Feb 2004 03:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:77745</guid><dc:creator>Yun Jin's WebLog</dc:creator><description /></item><item><title>Dangerous PInvokes - string modification</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#77760</link><pubDate>Sun, 22 Feb 2004 05:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:77760</guid><dc:creator>Yun Jin's WebLog</dc:creator><description /></item><item><title>Dangerous PInvokes - string modification</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#77789</link><pubDate>Sun, 22 Feb 2004 07:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:77789</guid><dc:creator>Yun Jin's WebLog</dc:creator><description /></item><item><title>re: Interning Strings &amp; immutability</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#113051</link><pubDate>Wed, 14 Apr 2004 16:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:113051</guid><dc:creator>David Goldstein</dc:creator><description>Chris,&lt;br&gt;There are times where if there were a safe place to have all your strings interned, you can write far more efficient code...&lt;br&gt;like in Object Spaces, they seem to assume that you can compare strings using == and do this many thousands of times per second as the cost of integer compares...&lt;br&gt;for my own O/R mapping projects, it's either use == for strings, find some painful way of avoiding strings altogether (source code generation) or generate dynamic assemblies (yes, this is the ideal).&lt;br&gt;</description></item><item><title>re: Interning Strings &amp; immutability</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#113123</link><pubDate>Wed, 14 Apr 2004 18:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:113123</guid><dc:creator>Chris Brumme</dc:creator><description>If you control the strings, you can explicitly intern them before proceeding with any comparisons (String.Intern, String.IsInterned).  Rather than going through operator== (which performs String comparisons on objects that are statically typed as String), you can use Object.ReferenceEquals (which just compares the two references).</description></item><item><title>re: Interning Strings &amp; immutability</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#148147</link><pubDate>Fri, 04 Jun 2004 06:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:148147</guid><dc:creator>William Stacey</dc:creator><description>Say you have a domainName hash table.  And users send domainName queries to it (e.g. dns)  You internal all ownerNames in all RRs.  Now you get a query with ownerName &amp;quot;www.test.com&amp;quot;.  Should you intern that string and compare with your hash of ownerNames?  That would seem fast, but would not the intern pool grow out of control as your adding strings to the pool even if ownername did not exist?  Do strings ever expire out of the intern pool if no ref to them?  TIA</description></item><item><title>re: Interning Strings &amp; immutability</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#148400</link><pubDate>Fri, 04 Jun 2004 16:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:148400</guid><dc:creator>Chris Brumme</dc:creator><description>If you Intern a string in an AppDomain, the Intern'ed string will remain until the AppDomain is unloaded.  So it's reasonable for you to Intern all the ownerNames, but it wouldn't be reasonable for you to Intern random test strings to see if you have a match.&lt;br&gt;&lt;br&gt;Instead, use the String.IsInterned method on the random test string.  If it isn't already interned, there's no point in trying to match it.  If it is interned already, then you aren't growing the set of Interned strings and you can now proceed with an efficient comparison against your Interned ownerNames.</description></item><item><title>re: Optimising Strings in .Net</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#215551</link><pubDate>Tue, 17 Aug 2004 10:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:215551</guid><dc:creator>Jase's Blog</dc:creator><description /></item><item><title>String Basics Part II</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#442263</link><pubDate>Sat, 23 Jul 2005 18:19:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:442263</guid><dc:creator>K. Scott Allen</dc:creator><description>The sample code in this post demonstrates that the run time maintains a string intern pool. The intern...</description></item><item><title>Shared Bytes, Private Bytes and Fixups</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#670635</link><pubDate>Wed, 19 Jul 2006 09:04:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:670635</guid><dc:creator>See Win App</dc:creator><description>This post is actually a re-post of a post I did a little under year ago during PDC '05 after attending...</description></item><item><title>Shared Bytes, Private Bytes and Fixups &amp;raquo; Wagalulu - Microsoft &amp;raquo;   &amp;raquo; Shared Bytes, Private Bytes and Fixups</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#670638</link><pubDate>Wed, 19 Jul 2006 09:09:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:670638</guid><dc:creator>Shared Bytes, Private Bytes and Fixups » Wagalulu - Microsoft »   » Shared Bytes, Private Bytes and Fixups</dc:creator><description>PingBack from &lt;a rel="nofollow" target="_new" href="http://microsoft.wagalulu.com/2006/07/19/shared-bytes-private-bytes-and-fixups/"&gt;http://microsoft.wagalulu.com/2006/07/19/shared-bytes-private-bytes-and-fixups/&lt;/a&gt;</description></item><item><title>.NET 2.0 string interning inside out</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#773455</link><pubDate>Wed, 27 Sep 2006 12:12:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:773455</guid><dc:creator>B# .NET Blog</dc:creator><description>Introduction&lt;br&gt;Time for some cool .NET 2.0 feature that might prove useful in some scenarios: string interning....</description></item><item><title>OJ&amp;#8217;s rants &amp;#8230;  &amp;raquo; Blog Archive   &amp;raquo; The Day Job</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#788841</link><pubDate>Wed, 04 Oct 2006 10:58:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:788841</guid><dc:creator>OJ’s rants …  » Blog Archive   » The Day Job</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://rant.blackapache.net/2006/10/04/the-day-job/"&gt;http://rant.blackapache.net/2006/10/04/the-day-job/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>  Strings Don&amp;#8217;t Add Up : C# 411</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#8052150</link><pubDate>Wed, 05 Mar 2008 17:34:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8052150</guid><dc:creator>  Strings Don’t Add Up : C# 411</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.csharp411.com/strings-dont-add-up/"&gt;http://www.csharp411.com/strings-dont-add-up/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Exigency In Specie / A Whole Load of Empty</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#9138308</link><pubDate>Mon, 24 Nov 2008 21:35:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9138308</guid><dc:creator>Exigency In Specie / A Whole Load of Empty</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.peat.me.uk/2007/06/01/a-whole-load-of-empty/"&gt;http://www.peat.me.uk/2007/06/01/a-whole-load-of-empty/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>String Basics in .NET | Mohammed Osman Blog US MS IT Hyderabad India</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#9257685</link><pubDate>Wed, 31 Dec 2008 01:15:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9257685</guid><dc:creator>String Basics in .NET | Mohammed Osman Blog US MS IT Hyderabad India</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://mdosman.us/?p=249"&gt;http://mdosman.us/?p=249&lt;/a&gt;&lt;/p&gt;
</description></item><item><title> cbrumme s WebLog Interning Strings and immutability | Quick Diets</title><link>http://blogs.msdn.com/cbrumme/archive/2003/04/22/51371.aspx#9745497</link><pubDate>Sat, 13 Jun 2009 17:22:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9745497</guid><dc:creator> cbrumme s WebLog Interning Strings and immutability | Quick Diets</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://quickdietsite.info/story.php?id=7394"&gt;http://quickdietsite.info/story.php?id=7394&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>