returnstring.Format("{0} is {1} years old and their favourite animal is {2}", Name, Age, AnimalsOrderedByFavourite().First());

//using string.format for formatting

returnstring.Format("Hamsters cost {0:0.00}", 14.22);

The problem with string.format is that it is possible to make mistakes with the format items, for example:

// this line works but outputs the wrong data. There is NO way for the compiler to identify this & you get no exceptions when you run it.

string.Format("{0} is {0} years old and their favourite animal is {2}", Name, Age, AnimalsOrderedByFavourite().First());

// this line compiles fine, but it will raise a X exception when run as there is only three parameters and your are asking for a forth

string.Format("{0} is {3} years old and their favourite animal is {2}", Name, Age, AnimalsOrderedByFavourite().First());

String Interpolation to the rescue!

String Interpolation aims to make these scenarios easier by allowing you to have blocks of code directly inside the string itself. To achieve this we need to tell the compiler that the string may contain blocks of code, and we use the $ symbol prefixed on the string to do that. We can then insert blocks of code using the same braces we normally use for blocks of code. This allows us to change to code in the above examples to:

var result = $"Hello {Name}";
var result2 = $"You {SubjectName} must be the pride and joy of {SubjectHomeTown}";
var result3 = $@"This was a {TestResult}.
I am making a {ReminderTool} here:
{SuccessRating}";
return $"{Name} is {Age} years old and their favourite animal is {AnimalsOrderedByFavourite().First()}";
return $"Hamsters cost {14.22:0.00}";

In the above examples, it becomes clear what member what are working with and it is not possible for us to make mistakes as the compiler will run and identify the issues at compile time which are problems! It can also simplify things as with the third line where we use a continuation without having to have many @ symbols scattered around.

This is Code

This isn’t just a way to insert properties into strings, it is a way to insert code so you can do all kinds of interesting things with this:

What about Cultures? The answer is IFormattable

Looking at example six in the “This is Code” section, you can see we use the string formatting for currency but we cannot specify the culture information, so we cannot specify HOW to format it if we want to be specific. What is the solution? The result of the the interpolated string is a string which also implements IFormattable, and you can create a method to set the correct culture very easily: