String Interpolation

The .NET languages allow you to replace portions of a string with values. To do so, you use String.Format or StringBuilder.AppendFormat. These methods allow you to use placeholders as numbers inside curly braces. These numbers are replaced in series by the values that follow. This is cumbersome to write and can lead to confusion.

In 2015, the code editor allows you to put the variable right in the middle of the string. You do so using the format that starts the string with a dollar sign ($) as an escape character. You can then add curly braces within the string to reference variables, as in {value}. The editor gives you IntelliSense for your values, too. The call to String.Format then happens for you behind the scenes. The example that follows shows how the previous use of String.Format is now simplified with enhanced string literals.

C#

//old style of String.Format
return String.Format("Name: {0}, Id: {1}", this.Name, this.EmpId);

//string interpolation style
return ($"Name: {this.Name}, Id: {this.EmpId}");

VB

'old style of String.Format
Return String.Format("Name: {0}, Id: {1}", Me.Name, Me.EmpId)

'string interpolation style
Return $"Name: {Name}, Id: {EmpId}"

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset