Robin Rottier
Robin Rottier's Blog


Robin Rottier's Blog


.NET 6 string format performance

Robin Rottier's photo
Robin Rottier
·Aug 13, 2021·

2 min read

Reading this article:

Very interesting from someone who knows what he's talking about, for string performance, in upcoming dotnet release.

My first thought actually is I am disappointed this is new stuff for .net 6 as I had (wrongly) assumed it would have behaved like this from the beginning. I had always thought that string interpolation was a great feature and would allow the compiler to preprocess the whole string and get rid of any calls to string.format.

If the compiler gets to parse the 'format' string into components (i.e. fixed string bits and expression bits inside ${}) then I would have though it could always whittle it down to a string.concat call of those separate component parts and not have to defer to a reconstructed string.format which then re-parses the whole string at runtime.

When I have reviewed code in the past a trivial thing but I hate more than anything is a developer who concatenates a string with code like:

Console.WriteLine(string.Format("This string comes from string a '{0}' and string b '{1}'", a, b));

so the string interpolation equivalent:

Console.WriteLine($"This string comes from string a '{a}' and string b '{b}'");

seems like it should always have put all the work onto the compiler and end up with best runtime string performance you could get via a string concat. For that simplest example seems it does but not sure why having a slightly more complex argument for one of those parts makes it defer to string.format for the whole thing.

But regardless previous performance happy if dotnet 6 is making it even better.

Share this