Note: If you are not familiar with String.Format and StringBuilder you can learn about it in my blog post C# String.
Recently, I saw some code that looked something like this:
Now, I don’t wana get into arguments about how String.Concat() is more performant here. String.Format() allows code to be more easily localized and it is being used for that purpose here. The real problem is that StringBuilder.AppendFormat() should be used instead:
The reason that this is important is because, internally, String.Format() actually creates a StringBuilder and calls StringBuilder.AppendFormat()! String.Format() is implemented something like this:
you can see the actual implementation in the source code for the runtime of .NET Core. Here is the link for the same: String.Manipulation.cs
In String.Manipulation.cs file you will find the Format method:
From the above code, you can see that Format method calls an internal method FormatHelper.
Now, the FormatHelper method uses StringBuilder for formatting the text using StringBuilderCache.
It turns out that the formatting logic is actually implemented in StringBuilder.AppendFormat(). So, the original code actually caused a second StringBuilder to be utilized when it wasn’t needed.
This is also important to know if you are trying to avoid creating a StringBuilder by concatentating strings with String.Format(). For example:
That code will actually create two StringBuilders, if the size of formatted string is greater than MaxBuilderSize, used in StringBuilderCache, which is set to 360. So, creating one StringBuilder and using AppendFormat() will be more performent:
I decided to run some performance tests to verify my claims. First, I timed code that demonstrates the very reason that StringBuilder exists:
The above code creates a new string and then concatenates to it inside of a for-loop. This causes two new strings to be created on each pass–one from String.Format() and one from the concatenation. This is woefully inefficient.
Next, I tested the same code modified to use a StringBuilder with String.Format():
Finally, I tested code that uses StringBuilder.AppendFormat() instead of String.Format():
These three methods ran with the following timings:
For .NET Framework:
For .NET Core:
Obviously, concatenating a string in a loop without using a StringBuilder is amazingly inefficient. And, removing the call to String.Format also yields a performance boost.
Next, I tested the following two methods:
These two methods ran with the following timings:
For .NET Framework:
For .NET Core:
As you can see, it is important to know when to use String.Format and when to use StringBuilder.AppendFormat(). While the performance boosts that can be achieved are fairly small, they are too easy to code.
You can download the performance tests here: https://github.com/kudchikarsk/string-format-performance-test.
Exploring memory allocation and strings by Maarten Balliauw - Strings are objects like any other object and follow the same rules. In this post, Maarten explains how they behave in terms of memory allocation.
string vs. String is not a style debate by Jared Parsons - Jared Parson who works on the C# compiler notes that String vs string is not a style debate. He makes a compelling case for why you should always use string.
Subscribe to Code with Shadman
Get the latest posts delivered right to your inbox