Interpolated Strings (C#)

3 January 2018

John (Redspa’s Head of Technical) looks into the Interpolated Strings feature of C# 6 and examines the pros and cons of using it.

What are Interpolated Strings?

Introduced in C# 6, Interpolated Strings allow you to cleanly construct strings from a mixture of hardcoded strings, variables and more.

Interpolated means “to add words to a text”. Let’s use an example to demonstrate using Interpolated Strings:
 

Example using Interpolated Strings

In older versions of C#, you could have used a number of ways to construct a piece of text using concatenation or formatted strings. For example:

// Using concatenation:
var concatenatedGreeting = “Hello “ + client.FirstName + “ “ + client.LastName;
// Using a formatted string:
var formattedGreeting = string.Format(“Hello {0} {1}”, client.FirstName, client.LastName);

Using Interpolated Strings, you can achieve the same result by simply using:

var clientFullName = $”Hello {client.FirstName} {client.LastName}”;

So when the client name is “Joe Bloggs”, all three output generating the following text:

Hello Joe Bloggs

In Interpolated Strings, the dollar sign ($) is used to tell the C# compiler that the string following it is to be interpreted as an Interpolated String. The curly braces encapsulate the values (of the variables) to include in the text. Note: you can include the literal curly braces by using “{{“ and “}}” accordingly.

All literal text is left in the string, such as the word “Hello” and all the whitespace.
 

Why should I use Interpolated Strings?

In my opinion, the Interpolated String example looks clean and makes it obvious as to how the separate parts of the text fit together (the word “Hello”, the client first name, the space character and the client last name). All the values are inline, improving readability.

Previously, I preferred to use formatted strings (i.e. string.Format) over concatenated strings as it improved the readability of the text you were trying to piece together. Formatted strings use placeholders (the values contains within curly braces), which Interpolated Strings have inherited and improved upon.

The positioning of the text and the use of whitespace is much more obvious (when using Interpolated Strings). I’ve come across many software bugs which could have been easily avoided using the clean approach provided by Interpolated Strings.

A big plus is that you get compile-time checking of your Interpolated Strings in Visual Studio. So if a variable value cannot be directly converted to a string, the compiler will let you know immediately, before your code goes anywhere near the Production website and your client base.

I’ve written unit tests that use Interpolated Strings to test the string output of business logic functions, and Interpolated Strings make it clear as to what the string output should be.

Finally, another nice feature is that you can mix in method calls, etc, into the placeholders (if you really want or need to!). For example:

var greeting = $”Hello {client.FirstName}!{IsClientBirthday(client) ? “ Happy birthday!” : string.Empty}”;


Are there any downsides in using Interpolated Strings?

As of this writing, Interpolated Strings are not (by default) supported in Razor views within MVC applications, which I think is a missed opportunity. However you can enable this feature manually using the advice in this StackOverflow article (when using C# 6 and MVC 6).

Also I think concatenated (and formatted) strings are a little more obvious to new developers, or those with a history in PHP (for example). But it’s worth putting in the time and practice using Interpolated Strings as it will help you write better code.


Summary

I highly recommend using Interpolated Strings, especially as it helps make code easier to read and be maintained by other developers. It also helps mitigate against annoying little text-orientated bugs (such as missing, or incorrect, whitespace appearing in text).


John Millard - Head of Technical About the Author
John Millard (Head of Technical at Redspa) has over 20 years commercial experience architecting and building web & software solutions. He specializes in cloud, web and backend technologies and has a keen interest in blogging, SEO and social media. Contact John at john@redspa.uk