C# 6 has been out for quite some time now and I thought I would write up a quick post on one of its newer features, which I use quite often: string interpolation.
What is String Interpolation?
String Interpolation isn't really anything new per se, it just provides a way of building the strings in a somewhat readable fashion, devoid of endless trails of '+' characters and the indexed placeholders.
Let's take a look at few ways, where you might actually concatenate the strings and we can see, where the string interpolation might come in.
The Classic Approach: Concatenation
I'm sure, every developer who reads this article at one point or another has chained tons of strings together with the concatenation operator '+' , as shown below:
- string a = "Han";
- string b = "shot";
- string c = "first";
- // Build your string
- var sentence = a + " " + b + " " + c + ".";
If you want to give the compiler a break and do some of the work yourself, you can use the String.Concat() method. This method accepts a set of parameters and does exactly, what you would imagine; it concatenates them efficiently:
- string a = "Han";
- string b = "shot";
- string c = "first";
- // Build your string
- var sentence = String.Concat(a," ",b," ",c,".");
You would simply instantiate an instance of the StringBuilder class and then use the methods like Append() and AppendLine() to add to your string, until finally outputing your end result via a ToString() call:
- var sb = new StringBuilder();
- for(var x = 1; x <= 5; x++)
- {
- sb.AppendLine(x);
- }
- var result = sb.ToString();
- Even Better: Formatting
In a scenario, we defined earlier, concatenation really isn't ideal. This is primarily true, because we have the values, which we are repeating (they are just spaces in this example, but they could not be) and are thus just wasting precious memory by defining them just to be concatenated.
The String.Format() method solves this issue by allowing us to build a "formatting string", which uses the Placeholders to define, where the additional parameters will be added to the string. It's not only more efficient than concatenating a large number of strings, but it can be quite readable as well:
- string a = "Han";
- string b = "shot";
- string c = "first";
- // Build your string
- var sentence = String.Format("{0} {1} {2}.",a,b,c);
- var sentence = String.Format("{0} {1} {2}; {0} shot Greedo.",a,b,c);
If there was any one of these options, which the String Interpolation feature is going to replace, it's going to be the String.Format() method. In fact, you can almost think of it, as a short-hand method for doing so.
The idea behind the string interpolation is that it removes the pesky placeholders, found in String.Format() calls and actually even removes any method calls. Roslyn compiler will do all of the work, when it recognizes, what is going on.
Let's take a look at our previous example again:
- var sentence = String.Format("{0} {1} {2}; {0} shot Greedo.",a,b,c);
- var sentence = $"{a} {b} {c}; {a} shot Greedo.";
Roslyn will actually read the values and map the variable names to their appropriate Placeholders. Additionally, these same Placeholders will support all of the common formatting strings, which are used for DateTime or the numerical input as well :
- var date = new Date(1977,5,25);
- var shooter = "Han";
- var victim = "Greedo";
- var shots = 42;
- var sentence = $"{shooter} shot {victim} {shots} times on a {date:dddd}.";
- var sentence = $"{shooter} shot {victim} {(shots > 1 ? shots + " times" : "once")} on a {date:dddd}.";
Pretty neat.
It's not a hammer.
While this new feature is something which I found myself using more and more, it isn't an all-out replacement for some of the other approaches. All of the operators and methods, which were previously mentioned will work just fine and have their own strengths.
Personally, the most common uses for the string interpolation have been involving the entities or objects and referencing the properties of them, similar to :
- var widget = WidgetFactory.GenerateWidget();
- Console.WriteLine($"Widget {widget.ID} was made on {widget.BuildDate}");
More about Strings
If you want some extended reading on the strings in general, discussions on concatenation, performance and more, check out some of these resources,

Vignesh ManiPosted Aug 17, 2016, 8:04 AM
Ncie one
Ghyath SerhalPosted Aug 16, 2016, 2:28 AM
Nice article