Click here to Skip to main content
15,879,474 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.33/5 (5 votes)
1 Sep 2015CPOL2 min read 12.1K   4   4
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.

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

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

C#
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.

C#
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
I'm from Pakistan and have two years of experience in C# and Asp.net in visual stdio 2005 ,2008, 2010 and SQL Server 2000/2005/2008

Comments and Discussions

 
BugWrong escape sequence Pin
Kim Nordmo2-Sep-15 5:12
Kim Nordmo2-Sep-15 5:12 
GeneralMy vote of 4 Pin
Dmitriy Gakh2-Sep-15 3:51
professionalDmitriy Gakh2-Sep-15 3:51 
GeneralRe: My vote of 4 Pin
idreeskhan2-Sep-15 4:49
idreeskhan2-Sep-15 4:49 
Hi,
This is not article, this tips and tricks.
GeneralRe: My vote of 4 Pin
Dmitriy Gakh2-Sep-15 6:00
professionalDmitriy Gakh2-Sep-15 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.