User Name:

User Email:

This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2021 - First Crazy Developer (Abhishek Kumar)

crazydeveloper New features in C# - String interpolation

New features in C# - String interpolation

C# 6.0 introduce with new feature "String Interpolation", which give you more control to format your string in easily way.

Before this feature when if we want to do this we used numbered placeholders like {0} in the format string, which must line up with separately supplied arguments:

string str = String.Format("{0} lives in {1} Delhi", emp.empName, emp.empAddress);

String interpolation give you power to put the expressions right in their place, by having “holes” directly in the string literal:

string str = "\{emp.empName} lives in \{emp. empAddress }";


string str = $"{emp.empName} lives in {emp. empAddress }";

We can also use optional alignment and format specifiers:

string studentdetails = $"\{student.Name,20} is \{student.Age:D3} year{s} old";

We can also use any expression with variables like:

string str = "{student.Name} is {(student.Age < 19 ? "not authorised to vote": "authorised to vote")}";

Notice that the conditional expression is parenthesized, so that the condition doesn’t get confused with a format specifier.

So, I hope in C# 6.0 you will enjoy with this new feature.

crazydeveloper Home Page 26 May 2015

Become a Fan