In MVC 6 source code I saw some code lines that has strings leading with $ signs.
As I never saw it before, I think it is new in C# 6.0. I'm not sure. (I hope I'm right, otherwise I'd be shocked as I never crossed it before.
It was like:
var path = $"'{pathRelative}'";
You're correct, this is a new C# 6 feature.
The
$
sign before a string enables string interpolation. The compiler will parse the string specially, and any expressions inside curly braces will be evaluated and inserted into the string in place.Under the hood it converts to the same thing as this:
Let's look at the IL for this snippet:
Which compiles to:
So the two are identical in the compiled application.
A note on the C# string interpolation syntax: Unfortunately the waters are muddied right now on string interpolation because the original C# 6 preview had a different syntax that got a lot of attention on blogs early on. You'll still see a lot of references to using backslashes for string interpolation, but this is no longer syntactically valid.