Click here to Skip to main content
12,405,543 members (65,334 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.2K views
7 bookmarked
Posted

Adventures in String Concatenation: Introducing String Interpolation

, 28 Oct 2015 CPOL
Rate this:
Please Sign up or sign in to vote.
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 that I use quite often: string interpolation.

Adventures in String Concatenation: Introducing String Interpolation

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 that I use quite often: string interpolation.

What is String Interpolation?

String Interpolation isn't really anything new per-say, it just provides a way of building strings in a somewhat readable fashion, devoid of endless trails of '+' characters and indexed placeholders.

Let's take a look at a few ways that you might actually concatenate strings and we can see where string interpolation might come in.

The Classic Approach: Concatenation

I'm sure every developer reading this blog at one point or another has chained tons of strings together with the concatenation operator '+' as seen below :

string a = "Han";  
string b = "shot";  
string c = "first";  
// Build your string
var sentence = a + " " + b + " " + c + ".";  

While this is fairly common, it is far from ideal. Concatenating a series of strings like this can yield all sorts of unexpected work for the compiler to do, but there really isn't anything wrong with it.

If you want to give the compiler a break and do some of the work yourself, then 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,".");  

This gets a bit trickier when you get into loops as the compiler may not be able to perform as much optimization as it would otherwise like. This is where the StringBuilder class comes in. StringBuilders allow you to continually and efficiently build a string without generating potentially hundreds of individual strings to do so.

You would simply instantiate an instance of the StringBuilder class and then use methods like Append() and AppendLine() to add to your string, until finally outputting 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 like we defined earlier, concatenation really isn't ideal. This is primarily true because we have values that we are repeating (they are just spaces in this example, but they could not be) and are thus just wasting precious memory defining them just to be concatenated.

The String.Format() method solves this issue by allowing us to build a "formatting string" that uses placeholders to define where 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);  

As you can see, this doesn't look like much, but if we wanted to expand it a bit, you could see how it might be more useful:

var sentence = String.Format("{0} {1} {2}; {0} shot Greedo.",a,b,c);  

Meet String Interpolation

If there was any one of these options that the String Interpolation feature is going to replace, it's going to be the String.Format() method. In fact, you could almost think of it as a short-hand method for doing so.

The idea behind string interpolation is that it removes the pesky placeholders found in String.Format() calls and actually even removes any method calls at all. The 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);  

This would simply result in "Han shot first; Han shot Greedo.". Now those placeholders aren't exactly pleasing to the eye, so let's replace this call with how it would be handled using String Interpolation:

var sentence = $"{a} {b} {c}; {a} shot Greedo.";  

That's it. Just append a '$' to the beginning of your string and include your variables within the placeholders.

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 that are used for DateTime or 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}.";  

This would output "Han shot Greedo 42 times on a Wednesday". But string interpolation isn't limited just to your normal formatting strings, you can also use some logic:

var sentence = $"{shooter} shot {victim} 
	{(shots > 1 ? shots + " times" : "once")} on a {date:dddd}.";  

This would output "Han shot Greedo 42 times on a Wednesday", however if you changed shots to 1, then it would read "Han shot Greedo once on a Wednesday".

Pretty neat.

It's not a hammer.

While this new feature is something that I find myself using more and more, it isn't an all out replacement for some of the other approaches. All of the operators and methods that were previously mentioned will work just fine and have their own strengths.

Personally, the most common uses for String Interpolation have been involving entities or objects and referencing properties of them similar to:

var widget = WidgetFactory.GenerateWidget();  
Console.WriteLine($"Widget {widget.ID} was made on {widget.BuildDate}");  

The most important thing to take away from this is the following: do not to compromise the readability of your code over things like formatting strings. If you find a case where this feature might be useful, then take advantage of it, but don't try to wedge it in, especially at the sake of readability.

More about Strings

If you want some extended reading on Strings in general, discussions on concatenation, performance and more, check out some of these resources:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rion Williams
Software Developer (Senior)
United States United States
An experienced Software Developer and Graphic Designer with an extensive knowledge of object-oriented programming, software architecture, design methodologies and database design principles. Specializing in Microsoft Technologies and focused on leveraging a strong technical background and a creative skill-set to create meaningful and successful applications.

Well versed in all aspects of the software development life-cycle and passionate about embracing emerging development technologies and standards, building intuitive interfaces and providing clean, maintainable solutions for even the most complex of problems.

You may also be interested in...

Comments and Discussions

 
QuestionYou should mention IFormattable and FormattableString too Pin
jho196516-Nov-15 10:57
memberjho196516-Nov-15 10:57 
QuestionPossible copyright issues Pin
Nelek9-Nov-15 2:12
protectorNelek9-Nov-15 2:12 
QuestionHow to know which version of c# i am using Pin
Tridip Bhattacharjee29-Oct-15 1:53
professionalTridip Bhattacharjee29-Oct-15 1:53 
AnswerRe: How to know which version of c# i am using Pin
MichP29-Oct-15 9:34
memberMichP29-Oct-15 9:34 
QuestionMy Vote of 5 Pin
nirman b doshi28-Oct-15 20:45
membernirman b doshi28-Oct-15 20:45 
QuestionThanks for sharing Pin
phil.o28-Oct-15 7:48
professionalphil.o28-Oct-15 7:48 
AnswerRe: Thanks for sharing Pin
Matt T Heffron28-Oct-15 11:38
professionalMatt T Heffron28-Oct-15 11:38 
GeneralRe: Thanks for sharing Pin
phil.o28-Oct-15 12:01
professionalphil.o28-Oct-15 12:01 
GeneralRe: Thanks for sharing Pin
Matt T Heffron28-Oct-15 12:05
professionalMatt T Heffron28-Oct-15 12:05 
GeneralRe: Thanks for sharing Pin
phil.o28-Oct-15 12:09
professionalphil.o28-Oct-15 12:09 
GeneralRe: Thanks for sharing Pin
Garth J Lancaster29-Oct-15 16:35
professionalGarth J Lancaster29-Oct-15 16:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 28 Oct 2015
Article Copyright 2015 by Rion Williams
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid