String Interpolation is a new feature to C# that was introduced in version 6. It allows you to create a string with one or more placeholders in it whereby the placeholders are then evaluated and replaced with values relating to variables within the available context. This can be used as an alternative to String.Format()

Using Variables

This is probably the simplest way to understand how String Interpolation can be used.

string s = "Craig";
string result = $"Hi, my name is {s}";

I am declaring s as "Craig", and result will be evaluated to "Hi, my name is Craig".

When declaring a variable with the type var from an interpolated string result, it will always return the string result.

var s = "String";
var result = $"This will return a {s}";

It is also possible to access variables which is an instance of a complex type.

var contact = contactService.Get(id);
var result = $"Welcome, {contact.Name}";

Using Methods

This is where you can start being creative on how you use this handy feature.

decimal i = 19.7m;
var result = $"Rounded result: {Math.Round(i)}";

This would give you the message: "Rounded result: 20".

You can also call methods on variables, such as

var transactions = transactionService.GetTransactions(id);
var result = $"Total transactions: {transactions.Count()}";

Expressions

Okay, this is where I say use with caution!

It is possible to do:

var transCount = transactionService.GetTransactions(id).Count();
var result = $"You have {( transCount > 0 ? $"{transCount} transactions" : "no transactions")}";

So the above interpolation would check if there are any transactions, and use the correct string result depending on the result of the condition. So the message could have been either:

  • You have 20 transactions
  • You have no transactions

I must admit, this is a nice feature, but you must ask yourself is this the cleanest and best approach to this? In some cases I would say it's quite nice to do, but I can see developers absolutely hating whoever leaves behind a complicated version of the above!

Custom Formatting

Sometimes you may want to output a value that isn't a string and you may find the result isn't exactly what you want. For example, you may want to output a financial balance which should be fixed by 2 decimal places.

var balance = account.GetBalance();
var result = $"Remaining Balance is {balance:F2}";

As IFormattable

You don't have to get the result as a string immediately. You can also declare the return type as a System.IFormattable. This is useful for internationalisation where you may want the result to be formatted different depending on the country or culture.

For example:

var price = product.GetPrice();
IFormattable formattable = $"The price of this item is {price:F2}";
var output = string.Format(
    System.Globalization.CultureInfo.CreateSpecificCulture("de-de"),
    fStr.Format,
    fStr.GetArguments());

If the price of the product was 12.95, the above should give you "The price of this item is 12,95"

Anyway, that was a short summary of what can be done with the new String Interpolation feature in C# 6. I look forward to using it anywhere I think it can simplify my code.