Skip to content

What is $" #18681

Closed
Closed

Description

@Vespiyanus

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.

Activity

  1. added
    help wanted
    and removed
    ⌚ Not Triaged
    on May 28, 2020
  2. IEvangelist commented on May 28, 2020

    @IEvangelist
    Member

    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"
    
  3. jmarkman commented on Jun 1, 2020

    @jmarkman
    Contributor

    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.

  4. IEvangelist commented on Jun 2, 2020

    @IEvangelist
    Member

    Hi @jmarkman - absolutely. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⌚ Not Triageddotnet-csharp/svchelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions