Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

String Interpolation or String Handling in C# 6.0 with New Feature

0.00/5 (No votes)
1 Sep 2015 1  
String interpolation or String handling in C# 6.0 with new feature

Introduction

This String interpolation new feature in C# 6.0 will help developers to save time and less effort and also will reduce the error chances.

Background

We all know when we want any string concat operation we used two methods. One simple way to add string is using + symbol and the second way we use string.Format which is much better than simple concatenation as this will create only one instance in memory.

Using the Code

A nice feature has been introduced by Microsoft C# team in a new version of C# 6.0 for String interpolation or you can say as String handling.

We being developers mostly use two ways to concat or add the string in C# existing versions.

First, one is simple using + but as you all know, this will create an instance of each concat inside memory while concatenating the string.

return "Student name is"+name+"and his date of birth is "+dob;  

To avoid this, mostly developers use the second way string.Format.

return string.Format("Student name is {0} and his date of birth is {1}", name, dob);

This was a simple example but when we have a long string and we want to concat multiple values, sometimes we forget to provide proper index and sometimes forget to provide proper value for specific index. In simple terms, we mixed up proper values and their index.

To get rid of this, Microsoft C# team has introduced a new feature in C# 6 for String interpolation and will help developers to save time and less efforts and will also reduce the error changes which we create due to improper index.

return $"Student name is {name} and his date of birth is {dob}";

This is the same as first was but instead of +" "+ it contain { } now the question is what is the benefit for that if we are again back to the same old way also we have to add $ symbol at the start of string.

Now how long string we have, we don't need to remember or worry about the index for the values and also it will create only single instance inside memory. So simply, we can say buy one get one free Smile | :) syntax remains easy as in the first case but with the feature of string.Format for creating a single instance.

$ will indicate if {start then next will be some sort of variable and put the value for that.

Now, the question is if we want really { } in our string, then what will we do.

The answer is very simple as we all know escape sequence in C# or adding the symbol twice.

return $"Student name is {{ idrees }} and his date of birth is {{09/09/1985 }}"

return $"Student name is \{name\} and his date of birth is \{dob\}";

The main benefit for this new feature will be very helpful in MVC and ASP.NET side especially. But this does not mean that we cannot use in any other type of app like Windows form, console and services, etc.

Note: This is an additional feature in C# 6.0 so all previous features remain as they are.

Points of Interest

When writing or displaying long and replacing the few variables inside, that will help to figure out where the exact value will be placed instead of index.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here