.Net String Pool – Not Just For The Compiler

B03B0055 On Monday, I was corrected in my assertion that creating multiple empty strings would create multiple objects.  Turns out the compiler automatically puts all of the strings that are exactly the same in a “string pool” so that there is only ever one empty string in the entire application you’ve created.

Duh! I should have known this, or at least I should have expected that this was so since it has been true with every other compiled language I’ve worked with.

But what I didn’t know and couldn’t expect is that we can make use of this string pool programmatically as well.

Why would you want to do this yourself?

Keep in mind that string concatenation in .NET requires the creation of a new object.  So, code such as this,

String a = "abcd";
String b = "efgh";
a += b;

creates a new object at line 3 every time it is executed.

So that if we add the following line:

b = "abcdefgh";

we would not be pointing to the same object.  That is, a and b would contain the same content but would be pointing to two entirely different objects.

if (a == b)
    Trace.Write("A and B contain the same data");

if (String.ReferenceEquals(a,b))
    Trace.Write("A and B are the same object");

String.Intern Consolidates The Data

By using String.Intern() we can get both evaluations to be true.

String a = "abcd";
String b = "efgh";
a = String.Intern(a + b);
b = "abcdefgh";

Now both evaluations above will be true because line 3 places the string “abcdefgh” in the pool and line 4 uses that same string from the pool to assign to b.  Where we might have created two objects, we are now only creating one and referring to it both times.

You could also use String.IsInterned(string) to determine if a string has already been placed in the string pool and execute optional code based on that.

When Would You Use This?

I still stand by my statement that optimizations should be saved for last.  You would not do this if this was the only place where you were doing the concatenation.  But you might consider doing this if your concatenations were in a loop that was taking a significant amount of processing time.

Other things you might also want to consider would be to consolidate concatenations on the same line and/or using the StringBuilder class for concatenations.  Keep in mind that StringBuilder is only really useful once you get past three concatenations due to the overhead of creating the StringBuilder object vs. creating new objects during the normal concatenation process.

Related Post

  • VB.NET – Char from String with Option StrictVB.NET – Char from String with Option Strict So here's the question: I'm using String.Split() and need to pass in a Char or a Char array as the parameter.  If I pass in a string String.Split("/") I get an error "Option Strict On disall...
  • C# “” better than string.Empty?C# “” better than string.Empty? I recently read an article that argued that “” is “Better than String.Empty” The argument is that since string.Empty doesn’t work in all situations, we should not use it at all.  He further a...
  • Using VB.NET From CSharpUsing VB.NET From CSharp Back when I was teaching .NET to other programmers regularly, I would frequently get the following question from a VB6 programmer moving to CSharp. “Is there any way of using some of the VB funct...
  • Forcing Extension Methods to Be UsedForcing Extension Methods to Be Used I received the following question: Is it possible to force a namespace to be included so that the extension methods in it will be used in it instead of the methods in the main class? I have to a...
  • Why you can’t cast an integer to a string.Why you can’t cast an integer to a string. I saw this question last Thursday on Channel 9 that I've heard before. My guess is that there are enough people who have the same question that it's worth addressing. I know there's probably a re...