Home » c# » .Net String Pool – Not Just For The Compiler

.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.


Other post in c#

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 […]
  • 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 […]
  • Why you can’t cast an integer to a string.Why you can’t cast an integer to a string. I saw a question 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 really good […]
  • CSharp Numeric OverflowsCSharp Numeric Overflows Did you know that when you are dealing with numbers, by default, .NET will do, or try to do exactly what you tell it to do?  If you tell it to do the impossible, it will do the next […]
  • VB.NET Processing Before WinForm DisplayVB.NET Processing Before WinForm Display I woke up this morning to an interesting question. “Using VB.net 2008, I want my project to be a Windows Forms Application, but upon startup, I want to check a few files to see if they […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS. Does your team need additional help in any of the above? Contact Dave today.

One Pingback/Trackback