Docs: What is $"

Created on 28 May 2020  Â·  3Comments  Â·  Source: dotnet/docs

For newbies, It would be very nice to see some explanations or note on $" (in Performing Operations on Source Elements section)


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide P3 up-for-grabs

Most helpful comment

I'd like to take a crack at refactoring this particular section to provide a bit more clarity for newer readers, if that's alright.

All 3 comments

Hi @Vespiyanus, thank you for raising this concern. Yes, I can see how it might be confusing to newbies. All of a sudden new and unfamiliar syntax is being used without any details about its usage.

I've added the "up-for-grabs" label on this issue, so that anyone could work on it before we have a chance to. To answer the ask, the $" is used for formatting strings, and is called string interpolation.

What it does, is format expressions within the string. It tells the C# compiler that the string should evaluate the expressions and then concatenate the pieces together.

double[] radii = { 1, 2, 3 };

IEnumerable<string> query =
    from rad in radii
    select $"Area = {rad * rad * Math.PI:F2}";

This is essentially shorthand for this "Area = " + (rad * rad * Math.PI).ToString("F2")

The query could then be iterated on, and it's just a string collection with the following values:

"Area = 3.14",
"Area = 12.57",
"Area = 28.27"

I'd like to take a crack at refactoring this particular section to provide a bit more clarity for newer readers, if that's alright.

Hi @jmarkman - absolutely. Thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ygoe picture ygoe  Â·  3Comments

Manoj-Prabhakaran picture Manoj-Prabhakaran  Â·  3Comments

sime3000 picture sime3000  Â·  3Comments

LJ9999 picture LJ9999  Â·  3Comments

tswett picture tswett  Â·  3Comments