10

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}'";
pauljz
  • 10,803
  • 4
  • 28
  • 33
Yves
  • 3,752
  • 4
  • 29
  • 43
  • 1
    @JeroenVannevel Yes it explains now what it is but the title of question is making it hard to find it and the accepted answer is old. So I think it is still worth to ask it? – Yves May 12 '15 at 15:13
  • 2
    This doesn't seem like a duplicate to me. Without already knowing what the $ is for you won't find that other question. – Richard Dalton May 12 '15 at 15:14
  • 2
    @RichardDalton: that doesn't mean it's not the same question. A rephrasing of the question is *not* a reason to duplicate it. It's not about the accepted answer either, it's about the 2nd answer which was added to update it. This question should remain closed. If you disagree, there's always http://meta.stackoverflow.com/ to discuss it further. – Jeroen Vannevel May 12 '15 at 15:15
  • 2
    @RichardDalton The fact that the OP couldn't find the duplicate on their own is all the more reason for it to be closed as a duplicate. Now they (and anyone else searching using the same terms used in this question) will be directed to that post where they can get their answer. – Servy May 12 '15 at 15:19
  • 5
    I have voted to reopen as it is not a "duplicate" - the second answer is a good _reference_ to find the correct answer but the question and accepted answer are completely irrelevant. I cannot find a question that asks whet the `$` operator is, so I think its fair to leave open - if you disagree then you are welcome to reclose. – D Stanley May 12 '15 at 15:23
  • 1
    @DStanley The duplicate answers the question. That's *exactly* what the duplicate feature is there for. – Servy May 12 '15 at 15:24
  • 1
    @Servy well I disagree, but it's a semantic argument and I'm at least down 3-1 that I know of. No more argument form me. – D Stanley May 12 '15 at 15:26
  • FWIW - I don't think `var path = $"'{path}'";` will compile as `path` has not been defined yet :) – D Stanley May 12 '15 at 15:27
  • @DStanley yeah you are right. I shortened it. It was longer. As I didn't know what the $ was, I couldn't know it. I'm editing it. – Yves May 12 '15 at 15:28
  • @Servy But still the accepted answer may confuse the searchers. Am I wrong? – Yves May 12 '15 at 15:29
  • 1
    @Yilmaz: then the searchers should read more than one answer. – Jeroen Vannevel May 12 '15 at 15:30
  • @RichardDalton I agree with you. That is why I upvoted your comment but still it may confuse... – Yves May 12 '15 at 15:31
  • 1
    I'm also voting to re-open this as it is in no way obvious how the original question is supposed to answer this question. – David Arno May 12 '15 at 15:31
  • @Yilmaz A confused reader is likely to continue reading, one that isn't, won't. – Servy May 12 '15 at 15:31
  • 1
    @DavidArno The fact that it's not obvious why it would doesn't change the fact that it *does*. The whole *point* of the duplicate feature is for duplicate content *that doesn't appear at first to be duplicate content* to still point to the same place. Content that's clearly just repeating exactly the same thing can just be *deleted*. – Servy May 12 '15 at 15:32
  • 4
    @Servy, that makes no sense. You have closed a question because there exists, in another question, an answer that answers this question, but which one must already know the answer in order to spot it is indeed an answer. You couldn't have come up with a more obscure reason to close a question if you'd tried. – David Arno May 12 '15 at 15:34
  • @DavidArno If someone has this question, and then goes to that question to find an answer, they'll find their answer. So what's the problem? They don't need to know which answer is the answer to their problem, it's immediately obvious from just glancing at them (without even reading them). – Servy May 12 '15 at 15:36

1 Answers1

18

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:

var path = string.Format("'{0}'", pathRelative);

Let's look at the IL for this snippet:

var test = "1";
var val1 = $"{test}";
var val2 = string.Format("{0}", test);

Which compiles to:

// var test = "1";
IL_0001: ldstr "1"
IL_0006: stloc.0

// var val1 = $"{test}";
IL_0007: ldstr "{0}"
IL_000c: ldloc.0
IL_000d: call string [mscorlib]System.String::Format(string, object)
IL_0012: stloc.1

// var val2 = string.Format("{0}", test);
IL_0013: ldstr "{0}"
IL_0018: ldloc.0
IL_0019: call string [mscorlib]System.String::Format(string, object)
IL_001e: stloc.2

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.

Community
  • 1
  • 1
pauljz
  • 10,803
  • 4
  • 28
  • 33