Introduction
Many software applications present formatted data to the user via user interfaces and reports. Often times the code that formats the data is difficult to read and maintain. This blog will briefly touch on a new C# 6.0 feature, String Interpolation, to format string data. In many cases, using string interpolation reduces the size and complexity of the code necessary to produce a formatted string.
String Formatting
Prior to C# version 6.0, programmers would use inline formatting or the string.format function to format strings.
An application has the requirement to put parenthesis around a string. To create functionality to meet this requirement, a good programmer would create a function that could be called upon when needed to process the string and return a string with surrounding parenthesis.
The architecture of this function would look like the following:
Inline Formatting
To put a set of parenthesis around a string using the inline formatting technique and the above function, one would create format a string as follows:
String.Format Formatting
The string.format function converts the value of objects to strings based on the formats specified and inserts them into another string. Using the string.format technique, the function would look like the following:
String Interpolation Formatting
In computer programming, string interpolation or variable interpolation (also variable substitution or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values. It is a form of simple template processing or, in formal terms, a form of Quasi-quotation (or logic substitution interpretation). String interpolation allows for easier and more intuitive string formatting and content-specification compared with string concatenation.*
String Interpolation is a new feature introduced in C# Version 6.0. Using string interpolation formatting, the function would look like the following:
Format specification is also supported using string interpolation. The following examples show how a string containing a date can be formatted inline, using string.format, and using string interpolation. The result of all three examples yields the following value.
The current date is Dec 3, 2015
Inline
var dataInLine = “The current date is “ + DateTime.Now.ToString(“MMM d, yyyy”);
String.Format
var dataInLineStringFormat = string.Format(“The current date is {0}“, DateTime.Now.ToString(“MMM d, yyyy”));
String Interpolation
var formattedDate = $”The current date is {DateTime.Now.ToString(“MMM d, yyyy”)}“;
Conclusion
This blog discussed how String Interpolation, a new feature introduced in C# 6.0, can be used to reduce the size and complexity of the code necessary to produce a formatted string, in comparison to Inline and String. By using String Interpolation, coders can reduce code size and complexity which leads to better code maintainability and a more robust product.
*https://en.wikipedia.org/wiki/String_interpolation