Rob Garrett - Blogs

Welcome to Rob Garrett - Blogs Sign in | Join | Help
in Search
Google

Software/Technology Discussion

Software and Technology Tid-bits

System.String

I've been reading some more of Applied Microsoft .NET Framework Programming this weekend. This book covers a lot about the .NET Framework that I already know, but delves deeper into topics, such as what happens at the CLR level, and how C# translates to IL. I was reading the chapter about strings on Saturday and decided to post a summary of what the author (Jeffrey Richter) covers, since not all developers are aware of what happens to strings under the hood.

Firstly, just to set the record straight, System.String is a reference type and not a value type, which means each string is allocated on the heap and not the stack. System.String is peculiar in that it has a shortcut keyword associated with it (string), which is a common practice with primitive value types - System.Int32 (int), System.Char (char) etc. String references also do not require the new keyword to allocate an instance, the following line of code will allocate space on the heap, fill it with string data and assign the reference to the variable:

Strings are immutable, meaning that once an instance of  System.String is created the contents of the object cannot be changed and is read-only. Immutable strings enable string references to be copied throughout an application without the need for thread synchronization.  A large number of string copies in an application require minimal overhead because only references to original string objects are copied and not the data itself.  Immutable strings come with a price, concatenation of multiple string references using the + operator generates temporary string allocations for each concatenation operation. The following code will generate at least 3 temporary strings on the heap before assigning the result to the result, also a string allocation. Using StringBuilder or StringWriter are preferred methods over string concatenation.

The Framework has a neat way of optimizing literal strings in an application. Each unique literal string reference in an application is stored in an internal hash table. Whenever a new string allocation is required the hash table is consulted first to determine if a reference is already available. Since strings are immutable two references to the same string object can coexists in the application without causing conflict. Dynamic created strings (e.g. strings that are the result of concatenation at runtime) are not added to the internal hash table and thus require a heap allocation to instantiate them. The Framework provides a static method called String.Intern, which will add a dynamic string to the hash table if not already interned. The String.IsInterned method behaves the same as String.Intern accept that it'll only return a reference to an interned string if it exists and not add the string to the hash table, otherwise it'll return null. Note, once a string is interned it is left in the hash table for the duration of the default AppDomain, there is no garbage collection of intern'd strings during the life of the application.

The following code demonstrates string intern:

Share this post: Email it! | bookmark it! | digg it! | reddit!
Published Monday, March 28, 2005 8:30 AM by Rob Garrett

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

Blurb


Head Shot
Rob Garrett is a British Expat living in Maryland USA. Rob is a trained software engineer and experienced in Windows .NET development.

Rob enjoys listening to Rock music, posting to blogs, driving in the country with the sunroof open, beer (not in conjunction with country driving) and spending time with his family.

This Blog

Syndication

Powered by Community Server, by Telligent Systems