String Interpolation vs String.Format

The answer is both yes and no. ReSharper is fooling you by not showing a third variant, which is also the most performant. The two listed variants produce equal IL code, but the following will indeed give a boost:

myString += $"{x.ToString("x2")}";

Full test code

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Diagnostics.Windows;
using BenchmarkDotNet.Running;

namespace StringFormatPerformanceTest
{
    [Config(typeof(Config))]
    public class StringTests
    {
        private class Config : ManualConfig
        {
            public Config() => AddDiagnoser(MemoryDiagnoser.Default, new EtwProfiler());
        }

        [Params(42, 1337)]
        public int Data;

        [Benchmark] public string Format() => string.Format("{0:x2}", Data);
        [Benchmark] public string Interpolate() => $"{Data:x2}";
        [Benchmark] public string InterpolateExplicit() => $"{Data.ToString("x2")}";
    }

    class Program
    {
        static void Main(string[] args)
        {
            var summary = BenchmarkRunner.Run<StringTests>();
        }
    }
}

Test results

|              Method | Data |      Mean |  Gen 0 | Allocated |
|-------------------- |----- |----------:|-------:|----------:|
|              Format |   42 | 118.03 ns | 0.0178 |      56 B |
|         Interpolate |   42 | 118.36 ns | 0.0178 |      56 B |
| InterpolateExplicit |   42 |  37.01 ns | 0.0102 |      32 B |
|              Format | 1337 | 117.46 ns | 0.0176 |      56 B |
|         Interpolate | 1337 | 113.86 ns | 0.0178 |      56 B |
| InterpolateExplicit | 1337 |  38.73 ns | 0.0102 |      32 B |

The InterpolateExplicit() method is faster since we now explicitly tell the compiler to use a string. No need to box the object to be formatted. Boxing is indeed very costly. Also, note that we reduced the allocations a bit.


Noticable is relative. However: string interpolation is turned into string.Format() at compile-time so they should end up with the same result.

There are subtle differences though: as we can tell from this question, string concatenation in the format specifier results in an additional string.Concat() call.


You should note that there have been significant optimizations on String Interpolation in C#10 and .NET 6 - String Interpolation in C# 10 and .NET 6.

I've been migrating all of my usage of not only string formatting, but also my usage of string concatenation, to use String Interpolation.

I'd be just as concerned, if not more, with memory allocation differences between the different methods. I've found that String Interpolation almost always wins for both speed and memory allocation when working with a smallish number of strings. If you have an undetermined (not known at design-time) number of strings, you should always use System.Text.StringBuilder.Append(xxx) or System.Text.StringBuilder.AppendFormat(xxx)

Also, I'd callout your usage of += for string concatenation. Be very careful and only do so for a small number of small strings.