Once in a while I receive e-mails from friends asking me about the best approach to handle a large number of strings, overhead during concatenation, garbage collector and so on. I decided to write this post to give some concepts and provide best practices on how to handle strings efficiently.
Just to warm up, let’s keep in mind that there is not magic formula since that it depends on what you are developing.
Facts to consider
1. Reference type – A string is a reference type made of an array of chars and its content is stored on the managed heap;
2. Strings are immutable on .NET – This means that each time you perform an operation on a string a new object is created. The last object to be created will retain the reference to the new memory space that now stores the result of the assignments while the temporary strings will be available to be released.
string str = string.Empty; str = "How "; str += "are "; str += "you?";
Code 1 – The simple code below creates 3 temporary strings in memory.
3. Temporary strings – The temporary strings created during the concatenation process will be released by the garbage collector. It results in more work to be performed by the garbage collector. More work means more time and resources spent.
The goal here is definitely to avoid unnecessary garbage collection performing an efficient string concatenation.
Efficient string concatenation
An efficient string concatenation depends at first on the operation you need to perform.
1. If you need to concatenate string literals like the code 1 give preference to the + sign to allow the concatenation happens in compile time:
string str = string.Empty; str = "How " + "are " + "you?";
Code 2 – String literals being concatenated in compile time.
2. If you need to concatenate string objects and you know the number of concatenations to be performed, give preference to functions like Concat, Format or Join:
string.Concat(str1, str2, str3);
Code 3 – Concat function.
3. If you have complex string concatenations and especially when you don’t know exactly how many concatenations you need to perform, like inside a loop to read content from a file or database give preference to a StringBuilder object:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < myArray.Length; i++)
/// Append some content
Code 4 – StringBuilder in use inside a loop.
Often we face the need to compare strings and I have seen a lot of strings being “prepared” to be compared. The drawback again is the creation of temporary strings. A very common usage is to convert the string to lower before perform the comparison.
string str1 = "ITtecture"; string str2 = "ittecture";
if (str1.ToLower() == str2.ToLower()) /// Do something...
Code 5 – Common usage of ToLower function before perform a string comparison.
For this particular purpose, we can use the Compare function to perform the task:
string.Compare(str1, str2, false)
Code 6 – Compare function.
The .NET framework provides a lot of functions to be used for a whole variety of string operation needs so keep in touch with the .NET documentation or MSDN website for a full list of them.
An efficient string handling is one of the many keys for a high quality and healthy code.
Add these tips into your toolbox and enjoy it.
See you on next post!