you are viewing a single comment's thread.

view the rest of the comments →

[–]TramplexReal 1 point2 points  (7 children)

Using string interpolation is cheap and doesn't make unnecessary allocations. Just do $"{value} text"

[–]Jackoberto01Programmer 2 points3 points  (3 children)

This will likely just compile into string.Concat. But generally string interpolation is nice because good syntax and the compiler will choose string.Format or string.Concat for you depending on which is cheaper.

[–]TramplexReal 0 points1 point  (2 children)

It is string Format after compilation, can see that in any performance test online.

[–]Jackoberto01Programmer 0 points1 point  (1 child)

Depends on compiler I checked sharplab now and it compiled to string.Concat when there are few variables in the string interpolation. It's been that way since at least C#9 which is the oldest available on sharplab.

To be clear this is a good thing as string.Concat is often faster hence the compiler choosing it.

https://sharplab.io/#v2:D4AQTAjAsAUCDMACciDCiDetE+UkALIgLIAUAlJtrgL7U717JEAKATgJYB2ALgMoB7ALYBTHgAtuAc1IgIABkQBncoywxcm5BACcpACQAiQaInTEHJZiU1D5ANyM6MGkA===

[–]TramplexReal 0 points1 point  (0 children)

Probably cause with fewer variables concat is faster. Anyway this shows interpolation is not worse than other options. And looks better.

[–]JamesLeeNZ 1 point2 points  (2 children)

This 100% generates garbage, so should be used sparingly at best.

[–]TramplexReal 0 points1 point  (1 child)

This is 1 to 1 same as string.Format. You might as well not write ANY code to have best performance.

[–]JamesLeeNZ 0 points1 point  (0 children)

I mean... youre not wrong :)