String Interpolation in C# 6.0

Posted on Posted in .net, C#.net

In our previous article, we have discussed the new features introduced in C# 6.0. You can go through that here.

In this article, you will learn:

  • String Concatenation in C# (Old mechanism vs New mechanism)
  • What is String Interpolation in C#?
  • How it works?

What is String Interpolation in C#?

String Interpolation is a mechanism to concatenate two or more strings together.

In older versions of C#, we were using “+” operator or String.Format method to concatenate strings but in C# 6.0, Microsoft has provided a feature named String Interpolation to concatenate strings.

Let’s see an example of Customer class where we will concatenate the first name and last name using traditional ways of concatenation.

 

Traditional ways of String Concatenation:

 

Using “+” Operator:

Using String.Format method:

New ways of String Concatenation:

 

Using String Interpolation:

By using the new String Interpolation feature in C# 6.0, you can :

  • You can place the String where ever you want it
  • You can use conditions
  • You can specify space before/after the string

 

So let’s see an example to implement above 3 scenarios using String Interpolation.

© 2015 – 2016, www.techkatak.com. All rights reserved.

2 thoughts on “String Interpolation in C# 6.0

  1. String interpolation is not working, i figure it out you are missing $sign before the string. e.g
    Console.WriteLine($”Name = {name}”)

Comments are closed.