What’s new in C# 6.0? - String Interpolation


Did you download the new Visual Studio 2015 IDE? If not, try the preview version before it actually releases and start improving your coding skills with the forthcoming IDE from Microsoft. Today we are going to explore another new feature of C# 6.0 coming with this release.

 

Let’s continue the series “What’s new in C# 6.0?” and show you what’s coming with C# 6.0 in terms of string interpolation/string concatenation.

 

 

Don’t forget to read my previous post on this series: “What’s new in C# 6.0? - Setting default values to Auto Properties”.

 

String concatenation – we regularly use it to show different records to the user. To do this, we either use the ‘+’ symbol or the string.Format method to format and organize various strings. This now becomes old days mechanism.

 

Old ways of string concatenation (www.kunal-chowdhury.com)

In C# 6.0, Microsoft added new feature to accelerate your coding skill. Now you can put expressions directly in the string literal to show the values in a proper formatted manner. You can also easily align or specify different format to your values. Not only this! You can now write conditions within string literals with additional strings as content.

 

Here are few examples of what we can do now with C# 6.0 in Visual Studio 2015 and .NET 4.6:

 

What's new in C# 6.0 - String Interpolation (www.kunal-chowdhury.com)

Hope you liked this feature. Microsoft is still working on this to improve a lot in string interpolation/string concatenation to add more easier way to provide conditions in strings. Wish to see them soon in the coming release of Visual Studio 2015.

 

It’s not the end. More to come on this series. Stay tuned to my blog for updates. Till that time, enjoy reading my other posts. Why don’t you subscribe to my blog’s RSS Feed and Email Newsletter? This will give you immediate update in your inbox. Connect with me over Twitter, Facebook and Google+ to get the updates which I share.

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

18 comments

  1. It would be helpful to see some scream shots on what these strings look like. I'm assuming the "\n" jazz is a newline. If my assumption is correct, why is one needed/desired at the very start of the output?

    Is an example of the first "new" interpolation displayed like this:
    [ blank line ]
    Fullname : Rupert Pupkin
    Joining: Rupert Pupkin

    ?

    ReplyDelete
    Replies
    1. Hi Clay,

      Yes you are right. Those "\n" used to create a new line.

      I am sorry! Those "\n" was used to create the demo app and then the screenshot was created directly from that. You can just ignore those line breaks. They don't have any meaning directly to the post.

      As you mentioned, the output will look similar to it.

      Hope that clarifies your query. Please let me know, if you have any further questions.

      Regards,
      Kunal

      Delete
  2. So, two characters to add the value to the string. What's the real gain over a "+ ?

    At least the plus operator was really clear in intent, while this new syntax can be really confusing without good highlighting.

    ReplyDelete
    Replies
    1. 1) Localization: strings with placeholders can be placed in a resource file. Using + means no localization is possible
      2) Maintainability: it's much easier to change a string in the source code without having to move around quotes and plus signs

      Delete
    2. Why do you need the '+' operator? We already have string.Format() method, which is better to use.

      Delete
  3. Why they didn't used Razor Syntax?

    ReplyDelete
    Replies
    1. Razor syntax is for mvc views only. This example is for c# non mvc apps (You could use it in an mvc app but if it's view related you should use razor).

      Delete
    2. This example deals with coding in c#. Razor only gets used in asp.mvc views. If it's view related then you should use razor in an mvc app. But there are other kind of apps where this is very usefull.

      Delete
    3. I think he meant why didn't they just subsume the razor syntax into c# for this

      Delete
  4. Perhaps you might want to correct the images showing the code in your blog? You do have the actual source code, right?

    ReplyDelete
  5. Thanks for sharing, looks like C# coding will get much easier in the times to come.

    ReplyDelete
  6. What is this crazy for wanting to write heaps of code all in one line?

    Come on guys! We're not back in the 1980's where code was relying on line numbers and had to squeeze in as much as we could in one statement.

    Expect code written this way to be unmaintainable...... and therefore error prone. KISS!

    ReplyDelete
  7. Nice feature but useless unless Microsoft implement a method to localize string patterns. Unfortunately I can not see any easy to implement that. This means we are stuck back to String.Format and .resx strings.

    ReplyDelete
  8. You're missing the leading '$' in your examples, took me a while to figure out why these weren't working. Should be something like:

    "Console.WriteLine($"FirstName : {p.FirstName}");"

    ReplyDelete
    Replies
    1. Great catch, you just saved me a very frustrating hour.

      Delete
  9. This comment has been removed by a blog administrator.

    ReplyDelete


 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top