Memory usage of concatenating strings using interpolated vs "+" operator

I made a simple test, see below. If you concatenate constants, don't use "string.Concat" because the compiler can't concatenate your strings at compile time. If you use variables, the results are effectively the same.

time measure results:

const string interpolation : 4
const string concatenation : 58
const string addition      : 3
var string interpolation   : 53
var string concatenation   : 55
var string addition        : 55
mixed string interpolation : 47
mixed string concatenation : 53
mixed string addition      : 42

the code:

void Main()
{

const int repetitions = 1000000; 
const string part1 = "Part 1"; 
const string part2 = "Part 2"; 
const string part3 = "Part 3"; 
var vPart1 = GetPart(1); 
var vPart2 = GetPart(2); 
var vPart3 = GetPart(3); 

Test("const string interpolation ", () => $"{part1}{part2}{part3}"); 
Test("const string concatenation ", () => string.Concat(part1, part2, part3)); 
Test("const string addition      ", () => part1 + part2 + part3); 
Test("var string interpolation   ", () => $"{vPart1}{vPart2}{vPart3}"); 
Test("var string concatenation   ", () => string.Concat(vPart1, vPart2, vPart3)); 
Test("var string addition        ", () => vPart1 + vPart2 + vPart3); 
Test("mixed string interpolation ", () => $"{vPart1}{part2}{part3}");
Test("mixed string concatenation ", () => string.Concat(vPart1, part2, part3));
Test("mixed string addition      ", () => vPart1 + part2 + part3);

void Test(string info, Func<string> action) 
{ 
    var watch = Stopwatch.StartNew(); 
    for (var i = 0; i < repetitions; i++) 
    { 
        action(); 
    } 
    watch.Stop(); 
    Trace.WriteLine($"{info}: {watch.ElapsedMilliseconds}"); 
} 

string GetPart(int index) 
    => $"Part{index}"; 

}

Strings are immutable. That means they can't be changed.

When you concatenate strings with a + sign, you end up creating multiple strings to get to the final string.

When you use the interpolation method (or StringBuilder), the .NET runtime optimizes your string use, so it (in theory) has better memory usage.

All that being said, it often depends on WHAT you are doing, and HOW OFTEN you are doing it.

One set of concatenations doesn't offer a lot of performance/memory improvements.

Doing those concatenations in a loop can have a lot of improvement.


Because strings in c# are immutable that's why same memory is used again and again so it does not impact memory much but in terms of performance you are actually differentiating between String.Format and String.Concat because at compile time your code will be like this

  string a = "abc";
  string b = "def";

  string.Format("Hello {0} {1}!", a, b);

  string.Concat(new string[] { "Hello ", a, " ", b, "!" });

there is a whole thread about performance between these two if you are interested String output: format or concat in C#


The author doesn't actually say that one makes better use of memory than the other. It says that the one method "makes good use of memory" in the abstract, which, by itself, doesn't really mean much of anything.

But regardless of what they said, the two methods aren't going to be meaningfully different in their implementation. Neither is going to be meaningfully different from the other in terms of memory or time.