In this tutorial, we will explore three approaches for including placeholders in our strings, and we will determine the best C# syntax for including expressions directly within our string assignments.
String Concatenation - A Convoluted Approach
As the C# programming language has evolved, so too have its features. Over the years, three primary methods of concatenating strings in C# have emerged. We will learn why each evolution of string formatting is an improvement over previous approaches.
Begin with a New Project called StringPlaceholders. We will start by exploring the classic way to concatenate strings. You will be familiar with this method from our previous tutorials.
string myName = "Brad";
int varX = 2;
int varY = 3;
int varSum = varX + varY;
Console.WriteLine("Hello " + myName + ". Adding " + varX + " + " + varY + " equals " + varSum +".");
Console.ReadLine();
//Result: Hello Brad. Adding 2 + 3 equals 5.
In this example, we have declared our variables and made all our calculations outside of the string assignment. We then use the concatenation operator ( + ) to piecemeal our string together one chunk at a time. It is a cumbersome approach and somewhat difficult to read. We have to be careful to match all the double quotes and keep up with spacing. It works, but it is easy to lose our place as we write the code.
Composite Formatting - A Step in the Right Direction
The next method is an improvement on classic string concatenation. With composite formatting, we are able to insert string placeholders and construct our string and reference our variables.
string myName = "Brad";
int varX = 2;
int varY = 3;
Console.WriteLine("Hello {0}. Adding {1} + {2} equals {3}.", myName, varX, varY, varX + varY);
Console.ReadLine();
//Result: Hello Brad. Adding 2 + 3 equals 5.
We are interested in the indexed replacement codes on Line 5. In our composite formatted string, we have added placeholders {0}, {1}, {2}, etc., everywhere we want to insert another item. After our string, we add comma-separated values corresponding to each indexed placeholder. The first variable in the list (myName
) will replace {0}
in the string, the second item (varX
) will replace {1}
, and so forth.
Item {3} on Line 5 demonstrates that we can perform calculations and insert the result via placeholder code directly into our string. Moreover, with composite formatting our string is easier to code because we don't have to deal with multiple double quotes and concatenation operators. However, our composite-formatted string is still somewhat difficult to read. To interpret the line, we must match each variable with each indexed placeholder.
Interpolated Strings - A Simple and Readable Syntax
String interpolation is an approach that was made available in C# version 6. This method improves upon traditional concatenation and composite formatting by providing a less complicated way to compose our strings while maintaining human readability of our code.
string myName = "Brad";
int varX = 2;
int varY = 3;
Console.WriteLine($"Hello {myName}. Adding {varX} + {varY} equals {varX + varY}.");
Console.ReadLine();
//Result: Hello Brad. Adding 2 + 3 equals 5.
In Line 5, you will see we began our string value with a dollar sign ( $ ). To designate a string as an interpolated string, we must prepend it with the $
symbol.
string myString = $"This is {myVar}.";
Once we have identified that we are working with an interpolated string instead of a literal string, we can insert items by placing them inside curly braces. Whereas a composite-formatted string requires indexed placeholders followed by a list of items for each placeholder, an interpolated string allows us to reference variable names (like myName
) or perform functions (varX + varY
) directly inside our string.
String interpolation is a convenient syntax for including expressions inside strings. You are not limited to simple variable replacements; you can embed any valid expression that returns a value and its result will automatically be converted and added to your string. The resulting code is simple and easier to read than the other string formatting options we have discussed.
Don't stop learning!
There is so much to discover about C#. That's why I am making my favorite tips and tricks available for free. Enter your email address below to become a better .NET developer.
Did you know?
Our beautiful, multi-column C# reference guides contain more than 150 tips and examples to make it even easier to write better code.
Get your cheat sheets