This is called string interpolation .
In C # 6 there is this symbol to indicate a special condition of that string literal. With string interpolation, we can use a variable or an expression within string . For this the compiler needs to understand that this is being used in it, that is, that it is a special literal. Without the symbol the compiler will interpret the literal as any text and will not take any specific action.
This is a way to avoid concatenating strings . It also avoids passing an argument for the string to be formatted:
WriteLine($"Contei {x} vezes");
WriteLine("Contei {0} vezes", x);
WriteLine("Contei " + x.ToString() + " vezes");
See working on dotNetFidle .
Comparing shapes
What's the best way?
The concatenation is certainly the worst and should be avoided, there were or there were few cases that it was useful. One advantage she had is that it was often faster to concatenate than to format a string by putting the content in.
The syntax difference becomes more evident outside Write
:
var texto = $"Contei {x} vezes";
var texto = string.Format("Contei {0} vezes", x);
The first one is simpler and maintains the reading flow. You see the expression that will be used to insert inside the text in the exact place where it will be inserted. With formatting (second example) you have to look ahead what will be the argument to be used. And if you have several, use some argument again and again, you'll have to keep counting to find out what goes in.
Note that the compiler evaluates what is within the tween. It is not treated as a part of the text but as part of the code. Any valid C # code can be used.
She does not solve everything
The second, the string formatting, is not discarded. It has the advantage of being solved at runtime. It's often what you want. A typical example is internationalization messages. You can have texts that can be in several languages. Within this text you must enter a variable that the application will put at the time of executing. This will only be resolved at runtime, so text interpolation is discarded.
Let's say you have the following text in an external file or database: Minha idade é {idade}
And there is a method that takes it in the file, like this:
var idade = 25;
var exemplo = GetLocalText("My age is"); //retorna o texto "Minha idade é {idade}"
var texto = $exemplo; //isto não funciona, não é um literal.
This text of the exemplo
variable coming from outside the application is not known to the compiler. The idade
inside the text has never been compiled, so the variable can not be placed in the place of this text.
The solution would be to save the different string and pass an argument to it.
var idade = 25;
var exemplo = GetLocalText("My age is"); //retorna o texto acima
var texto = string.Format(exemplo, idade); //produzirá "Minha idade é 25".
Of course if you are going to repeat an expression within the text it is probably worth doing it before saving it to a variable and using only the variable.
Additional information
Sample documentation and other forms of formatting .
An example of how it fits better with other ASP.NET Core features.
Old:
@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index",
"Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
New:
<a asp-controller="Manage" asp-action="Index" title="Manage">Hello @User.GetUserName()!</a>
Retired from this article .
To get this result internally there is an interpolation.