The best way to find out is by testing. I did a simple test:
using static System.Console;
using System.Diagnostics;
public class Program {
public static void Main() {
var x = "";
var sw = new Stopwatch();
sw.Start();
for (var i = 0; i < 10000; i++) x = string.Format("teste de string formatada com resultado: {0}", i + 5);
sw.Stop();
WriteLine(sw.ElapsedTicks);
sw.Restart();
for (var i = 0; i < 10000; i++) x = $"teste de string formatada com resultado: {i + 5}";
sw.Stop();
WriteLine(sw.ElapsedTicks);
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
The best test will be done in a controlled environment and not on a shared server where you don’t know what is running. I’ve run it a few times. And overall the result was almost the same.
But it is important to note that in other patterns this result may be different. This kind of test only says about this particular situation, but it can generalize. It may be that mounting the test otherwise gives a different result, even if it performs the same thing. To properly test it is necessary to deeply understand the implementations to try to find the points where each one stands out and where they fail. If the implementation changes, the test no longer serves.
I put in the Sharplab to see the generated code and both use the Format()
.
More information on documentation. As it is not in the documentation is implementation detail and one day can change.
The great advantage is not in speed, but in simplification and convenience. And Resharper suggests because it is more elegant, not because it is faster.
In C# 10, or another if you are late, you will have the string constant interpolated that will bring a difference in performance. See more.