C# 6.0–String Interpolation

This blog post is part of C# 6.0 Features Series.
As we all know, C# 6.0 provides lots of small enhancements to the language and string interpolation is one of them. In this blog post we are going to learn how this small new feature can help us for string concatenation.

So let’s take small example, Where I have created a new example of string concatenation. Following is a code for that.
using System;
namespace StringInterpolation
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "Jalpesh Vadgama";

            //Old way of string concentaionation 
            Console.WriteLine("My name is" + name);
            Console.WriteLine("My name is {0}", name);

            //New way of doing this
            Console.WriteLine("My name is {name}");
        }
    }
}

Here if you see above code you will understand that I have written both way of doing string concatenation. Earlier we have used to do concatenation with + operator or we were using string.format place holder for variable like “{0}” and then put that variable in function itself. But in the new way of string concatenation you can put directly variable with “\{your variable name}”.

With this way code look more beautiful and readable. Isn’t it and when you run this application it will works fine like below.

csharp6-string-interpolation

That’s it. Hope you like it. Stay tune fore more!.

Note: You can get code of all example of C# 6.0 new features at following location on github.https://github.com/dotnetjalps/Csharp6NewFeatures

Comments

Popular posts from this blog

How to create Rest API(Web API) with ASP.NET Core 1.0

Creating PDF with ASP.Net MVC and RazorPDF

Solution: gulp is not recognized as internal or external command- Visual Studio