Is there a noticable performance difference between using string interpolation:
myString += $"{x:x2}";
vs String.Format()?
myString += String.Format("{0:x2}", x);
I am only asking because Resharper is prompting the fix, and I have been fooled before.
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.
string interpolation is turned into string.Format() at compile-time.
Also in string.Format you can specify several outputs for single argument, and different output formats for single argument. But string interpolation is more readable I guess. So, it's up to you.
a = string.Format("Due date is {0:M/d/yy} at {0:h:mm}", someComplexObject.someObject.someProperty);
b = $"Due date is {someComplexObject.someObject.someProperty:M/d/yy} at {someComplexObject.someObject.someProperty:h:mm}";
There is some performance test results https://koukia.ca/string-interpolation-vs-string-format-string-concat-and-string-builder-performance-benchmarks-c1dad38032a
来源:https://stackoverflow.com/questions/32342392/string-interpolation-vs-string-format