Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. C# interpolated string, JavaScript template literal

C# interpolated string, JavaScript template literal

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpquestionjavascriptcomhelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • raddevusR Offline
    raddevusR Offline
    raddevus
    wrote on last edited by
    #1

    You probably know about C# String interpolation and the new(er) (C# 6, via .NET 4.6) string operator $. Here's the explanation and sample from ($ - string interpolation (C# Reference) | Microsoft Docs[^])

    string name = "Mark";
    var date = DateTime.Now;

    // Composite formatting:
    Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
    // String interpolation:
    Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
    // Both calls produce the same output that is similar to:
    // Hello, Mark! Today is Wednesday, it's 19:40 now.

    The new formatting is very nice, much more straight forward and obvious about which values are included in the string. JavaScript Analog But, did you know about the JavaScript equivalent that entered in JavaScript 2015? I just saw this for the first time -- only three years late. :laugh: JavaScript calls them template literals. The very odd thing is that you have to use back-ticks to get them to work.

    var who = "world";
    console.log(`hello, ${who}`);

    That produces an output of :

    hello, world

    If you use double or single quotes instead of back-ticks it doesn't work. The back-ticks kind of make me shudder. :| It is a bit nicer than concatenating strings and vars with all those + signs though. Previously you had to do something like:

    var who = "world";
    console.log("hello, " + who);

    You can read more about template literals here[^]. It's interesting to see a concept emerge and then how different language developers implement it. C# Bonus Weirdness You can use the verbatim string operator with the interpolated string operator like the following:

    string finalAnswer = $@"this is the answer to the question: {40+2}";
    Console.WriteLine(finalAnswer);

    But if you place the verbatim string operator before the interpolated string operator you will get a syntax error.

    string finalAnswer = @$"thi

    F Richard DeemingR 2 Replies Last reply
    0
    • raddevusR raddevus

      You probably know about C# String interpolation and the new(er) (C# 6, via .NET 4.6) string operator $. Here's the explanation and sample from ($ - string interpolation (C# Reference) | Microsoft Docs[^])

      string name = "Mark";
      var date = DateTime.Now;

      // Composite formatting:
      Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
      // String interpolation:
      Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
      // Both calls produce the same output that is similar to:
      // Hello, Mark! Today is Wednesday, it's 19:40 now.

      The new formatting is very nice, much more straight forward and obvious about which values are included in the string. JavaScript Analog But, did you know about the JavaScript equivalent that entered in JavaScript 2015? I just saw this for the first time -- only three years late. :laugh: JavaScript calls them template literals. The very odd thing is that you have to use back-ticks to get them to work.

      var who = "world";
      console.log(`hello, ${who}`);

      That produces an output of :

      hello, world

      If you use double or single quotes instead of back-ticks it doesn't work. The back-ticks kind of make me shudder. :| It is a bit nicer than concatenating strings and vars with all those + signs though. Previously you had to do something like:

      var who = "world";
      console.log("hello, " + who);

      You can read more about template literals here[^]. It's interesting to see a concept emerge and then how different language developers implement it. C# Bonus Weirdness You can use the verbatim string operator with the interpolated string operator like the following:

      string finalAnswer = $@"this is the answer to the question: {40+2}";
      Console.WriteLine(finalAnswer);

      But if you place the verbatim string operator before the interpolated string operator you will get a syntax error.

      string finalAnswer = @$"thi

      F Offline
      F Offline
      Forogar
      wrote on last edited by
      #2

      Quote:

      if you place the verbatim string operator before the interpolated string operator you will get a syntax error.

      That's because the verbatim operator is tightly couple to the string and suppresses interpolation of the string - therefore the interpolation operator needs to come after the resulting string from the "non-interpolation" operator. ...erm, or maybe the other way around? Whatevs!

      - I would love to change the world, but they won’t give me the source code.

      raddevusR 1 Reply Last reply
      0
      • F Forogar

        Quote:

        if you place the verbatim string operator before the interpolated string operator you will get a syntax error.

        That's because the verbatim operator is tightly couple to the string and suppresses interpolation of the string - therefore the interpolation operator needs to come after the resulting string from the "non-interpolation" operator. ...erm, or maybe the other way around? Whatevs!

        - I would love to change the world, but they won’t give me the source code.

        raddevusR Offline
        raddevusR Offline
        raddevus
        wrote on last edited by
        #3

        Yeah, the way I saw it explained was : $ then @ because you are saying, "interpolate this verbatim string..." Instead of @ then $ "Give me verbatim of this interpolated string..." Kind of makes sense, I guess.

        M 1 Reply Last reply
        0
        • raddevusR raddevus

          Yeah, the way I saw it explained was : $ then @ because you are saying, "interpolate this verbatim string..." Instead of @ then $ "Give me verbatim of this interpolated string..." Kind of makes sense, I guess.

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #4

          raddevus wrote:

          Kind of makes sense, I guess.

          I promise to quote you verbatim without interpolation. ;)

          Latest Article - A Concise Overview of Threads Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

          raddevusR 1 Reply Last reply
          0
          • M Marc Clifton

            raddevus wrote:

            Kind of makes sense, I guess.

            I promise to quote you verbatim without interpolation. ;)

            Latest Article - A Concise Overview of Threads Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

            raddevusR Offline
            raddevusR Offline
            raddevus
            wrote on last edited by
            #5

            Marc Clifton wrote:

            I promise to quote you verbatim without interpolation.

            // no comment

            :rolleyes:

            1 Reply Last reply
            0
            • raddevusR raddevus

              You probably know about C# String interpolation and the new(er) (C# 6, via .NET 4.6) string operator $. Here's the explanation and sample from ($ - string interpolation (C# Reference) | Microsoft Docs[^])

              string name = "Mark";
              var date = DateTime.Now;

              // Composite formatting:
              Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
              // String interpolation:
              Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
              // Both calls produce the same output that is similar to:
              // Hello, Mark! Today is Wednesday, it's 19:40 now.

              The new formatting is very nice, much more straight forward and obvious about which values are included in the string. JavaScript Analog But, did you know about the JavaScript equivalent that entered in JavaScript 2015? I just saw this for the first time -- only three years late. :laugh: JavaScript calls them template literals. The very odd thing is that you have to use back-ticks to get them to work.

              var who = "world";
              console.log(`hello, ${who}`);

              That produces an output of :

              hello, world

              If you use double or single quotes instead of back-ticks it doesn't work. The back-ticks kind of make me shudder. :| It is a bit nicer than concatenating strings and vars with all those + signs though. Previously you had to do something like:

              var who = "world";
              console.log("hello, " + who);

              You can read more about template literals here[^]. It's interesting to see a concept emerge and then how different language developers implement it. C# Bonus Weirdness You can use the verbatim string operator with the interpolated string operator like the following:

              string finalAnswer = $@"this is the answer to the question: {40+2}";
              Console.WriteLine(finalAnswer);

              But if you place the verbatim string operator before the interpolated string operator you will get a syntax error.

              string finalAnswer = @$"thi

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              raddevus wrote:

              But if you place the verbatim string operator before the interpolated string operator you will get a syntax error.

              Looks like that should be "fixed" in C# 8: Implement verbatim interpolated strings by jcouv · Pull Request #28355 · dotnet/roslyn · GitHub[^] Champion: verbatim interpolated string (order of $@ vs. @$) · Issue #1630 · dotnet/csharplang · GitHub[^]


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              raddevusR 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                raddevus wrote:

                But if you place the verbatim string operator before the interpolated string operator you will get a syntax error.

                Looks like that should be "fixed" in C# 8: Implement verbatim interpolated strings by jcouv · Pull Request #28355 · dotnet/roslyn · GitHub[^] Champion: verbatim interpolated string (order of $@ vs. @$) · Issue #1630 · dotnet/csharplang · GitHub[^]


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                raddevusR Offline
                raddevusR Offline
                raddevus
                wrote on last edited by
                #7

                Richard Deeming wrote:

                Looks like that should be "fixed" in C# 8:

                :thumbsup: Thanks for the update and links.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups