I have 2 codes in C# 6.0:
Sample 1:
string bar;
// ... some code setting bar.
var foo =
"Some text, " +
$"some other {bar}, " +
"end text.";
Sample 2:
string bar;
// ... some code setting bar.
var foo =
$"Some text, " +
$"some other {bar}, " +
$"end text.";
Obviously both codes generate the same result regardless of aesthetic differences.
Question:
Is there a performance difference between both? In other words, are both cases compiled to the same?
EDIT:
Some important comments was posted that I find useful to replay to make a clarification of the topic in this question.
The second one is horrible. Don't use the string interpolation symbol ($) on strings that aren't interpolated. It will be confusing for anyone else looking at the code (or future, more experienced you)
Thanks for the tip, it's very appreciated, although I have to argue that what you said is a aesthetic argumentation. Even this code been as ugly as you said, even if should never been used, I still think is useful discuss possible performances differences that It MAY generated.
@communityMember1: It doesn't affect performance of the code at runtime.
@communityMember2: That is not true. Interpolated string is syntactic sugar for calling string.Format, which has to be called (and thus affecting performance).
Maybe to avoid further discussion, we should reinforce the answers and comments based on some documentation or even an empirical proof (like the result of a compilation).