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";
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#
- Assign Multiple enum Values To One Variable - November 2nd, 2007
- Simple Properties in C# 3.5 - November 22nd, 2007
- CSharp adds the var keyword! - November 27th, 2007
- Object Initialization in CSharp 3.0 and VB.NET 9 - November 28th, 2007
- Computed Columns Using LINQ to SQL Classes - February 12th, 2008
- DataSets, ObjectDataSource and 3-Tier ASP.NET - February 29th, 2008
- Serializable attribute vs Serializable Interface - April 9th, 2008
- Why you can't cast an integer to a string. - June 23rd, 2008
- Unsafe Mode in C# - December 15th, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- Upload a File via WebRequest Using CSharp - November 10th, 2009
- Azure CDN Blob Storage Case Sensitive Issue - April 17th, 2012
- && vs & and | vs ||... What's the difference? - August 21st, 2012
- Decimal vs Float (Single) or Double - September 18th, 2012
- C# Self Executing Anonymous Function - February 28th, 2013
- Dispose, Finalize and SuppressFinalize - June 12th, 2013
- CSharp Enum - October 2nd, 2013
- CSharp Numeric Overflows - July 3rd, 2014
- CSharp IDisposable Confusion - July 17th, 2014
- Why Get Certified? - July 24th, 2014
- C# Properties Get and Set - November 13th, 2014
- Value Type vs Reference Type - November 27th, 2014
- String and StringBuilder - December 11th, 2014
- Browser Automation in .NET w/ Chromium - July 9th, 2015
- CefSharp Offscreen [Why do I have so many instances of Chromium?] - October 1st, 2015