I went through my entire C # reference book and could not find what '$' is.

I understood only that this is somewhat similar to the verbatim string '@'.

Console.WriteLine($"?"); 

How does this affect the string?

2 answers 2

The prefix $ in front of the line means that the interpolated line comes next.

Interpolation is used to conveniently construct a string. At the same time, the expression of interpolated lines can look like a line template, in which language expressions can be used.

Interpolated strings are easier to understand with respect to arguments, compared to composite formatting.

Example:

 var s = $"Name = {name}, hours = {hours:hh}"; 

Compared to string.Format

 var s = string.Format("Name = {0}, hours = {1:hh}", name, hours); 

An important difference from the same string.Format :

String interpolation is transformed at. This is where you can find out how you’d like to see it.

The interpolated string at the time of compilation is transformed into an equivalent call to string.Format . This leaves the localization support as before (although still with the traditional formatting of strings) and does not allow you to add any code injection to the string after compilation.

This feature came with the release of C # 6 .

You can read more in MSDN Help.

  • String.Format a specification that says, or just an implementation detail? - Qwertiy
  • @Qwertiy, this is how it is written in the article, it didn’t look in the specification what was happening - Grundy

This is string interpolation, a handy thing that allows you to write an expression:

 int i=1; Console.WriteLine($"i={i}"); 

Learn more here: https://msdn.microsoft.com/ru-ru/library/dn961160.aspx

  • one
    @tCode, not a replacement - Grundy
  • four
    @tCode The difference with string.Format is that the pattern is resolved at compile time (literal), and in Format it can be pushed dynamically. - free_ze
  • one
    Well, this is an important point, so the interpolated line forms the line faster, but it does not allow to use another output template in runtime. - Sergey
  • one
    @Grundy for most applications is a replacement. Well, yes, incomplete replacement is obtained. But the replacement is the same! - Pavel Mayorov
  • one
    @PavelMayorov, and how do you want to do this in the interpolated line? The compiler cannot merge substitutions. - Qwertiy