What does the symbol "$" mean before a string?

Asked

Viewed 2,641 times

49

Viewing a code here in Sopt, I noticed the use of the symbol "$" and I was left with doubt about its use.

  • What is the symbol "$" before a string?
  • What good is he?
  • Why use it?

Example

using static System.Console;

public class Program {
    public static void Main() {
        var texto = " Hello World ";
        WriteLine($"Corta o fim: |{texto.TrimEnd()}|");
        WriteLine($"Corta o início: |{texto.TrimStart()}|");
        WriteLine($"Corta ambos: |{texto.Trim()}|");
        WriteLine($"Corta tudo: |{texto.Replace(" ", "")}|");
    }
}

1 answer

53


That’s called string Interpolation.

In C# 6 there is this symbol to indicate a special condition of that literal string. With the interpolation of string, we can use a variable or an expression within the 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 take no specific action.

It’s a way to avoid the concatenation of strings. It also avoids the passage of an argument to the string be formatted:

WriteLine($"Contei {x} vezes");
WriteLine("Contei {0} vezes", x);
WriteLine("Contei " + x.ToString() + " vezes");

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Comparing the forms

What is the best way?

Concatenation is certainly the worst and should be avoided, there were or were few cases that it was useful. One advantage she had is that it was often faster to concatenate than to format a string putting the content inside.

The syntax difference is most evident outside the Write:

var texto = $"Contei {x} vezes";
var texto = string.Format("Contei {0} vezes", x);

The first is simpler and keeps the reading flow. You see the expression that will be used to insert within the text in the exact place where it will be inserted. With the formatting (second example) you have to look ahead to what argument will be used. And if you have several, use some argument over and over again, you have to keep counting to find out what goes where.

Note that the compiler evaluates what is within the interpolation. That is not treated as a part of the text but as part of the code. Any valid C# code can be used.

She doesn’t solve everything

The second, the string formatting, it is not discarded. It has the advantage of being solved at runtime. It is often what is desired. A typical example is internationalization messages. You can have texts that can be in multiple languages. Inside this text must enter a variable that the application will put when executing. This will only be resolved at runtime, so text interpolation is discarded.

Let’s say you have in an external file or database the following text: Minha idade é {idade} And there’s a method that picks it up 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 variable exemplo from outside the application is not known to the compiler. The idade there inside the text has never been compiled, so the variable cannot be put in place of this text.

The solution would be to keep the string different (Minha idade é {0}) and pass an argument to her.

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 executing before saving it in a variable and using only the variable.

Extra information

Documentation with example and other forms of formatting.

An example of how it looks better together with other ASP.NET Core features.

Ancient:

@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>

I put in the Github for future reference.

Taken from that article.

To get this result internally there is an interpolation.

In future version of C# (maybe 10) must have the interpolation of string constant which is an optimization I’ve been waiting for since the 6.

  • 6

    +1. I had never used this Feature. Living and learning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.