C# 6 Features: String Interpolation
by Joseph on Jan 29th in .Net, C#, Visual Studio
I had a few minutes, so I thought I’d get a series of posts started – really quick snippets about why C# 6 features are interesting, and why they matter.
The one I’m going to start with is the one I’ve been using far and away the most. String Interpolation.
Take a look at this:
[gist id=”cf1e483aa9d25dc76196″ file=”StringInterpBefore.cs”]
It’s pretty basic. We’re instantiating a Person (remember him, from our last post?) and showing the properties in the console window. Nothing fancy.
In prior to C# 6, this involvedĀ either using String.Format() or, if you were really crazy doing something like this:
[gist id=”cf1e483aa9d25dc76196″ file=”StringConcatCrazy.cs”]
Now, however, we have something new. String interpolation!
By just putting a Dollar Sign “$” in front of the string, we can insert adhoc bits of code into the string.
This can be dangerous. I could see it leading to some funky code.
As with so much in life, with great power comes great responsibility…
Take a look at how it ends up with our simple example.
[gist id=”cf1e483aa9d25dc76196″ file=”StringInterpAfter.cs”]
Other Posts in the Series