C# – String Interpolation

This tutorial will explain what string interpolation is and how to use it in the C# programming language. The tutorial will also compare strings created with string interpolation to strings created by concatenation and composite formatting to show the benefits of using string interpolation.

String Concatenation

Lets start by reviewing string concatenation. If we need to create a string that contains the value of a variable we can use the plus sign (+) to join one or more string literals to one or more variables. The below code shows an example of using concatenation.

string fname = "Jessie";
string mname = "Andrew";
string lname = "Edmisten";
int age = 39;
string job = "Software Developer";

// Example of string concatenation
Console.WriteLine("Hello, my name is " + fname + " " + mname + " " + lname + ". I am " + age + " years old. I am a " + job + ".");
Output of string concatenation

You can see from this example that creating a string literal by using concatenation gives us the output that we need. However, it is prone to errors because it is easy to forget the spaces before or after variables and it is also easy to forget the period at the end of the sentence. I actually needed to update this code after I wrote it because I forgot to add the space after the age variable.

Composite formatting

Next we will review composite formatting. When creating a string with composite formatting, curly braces with a number inside them are used as place holders for variables. The placeholders are numbered starting with zero. A comma is used to separate the string from an argument list of variables. Each placeholder needs to match the corresponding variable in the argument list. The below code shows an example of using composite formatting.

string fname = "Jessie";
string mname = "Andrew";
string lname = "Edmisten";
int age = 39;
string job = "Software Developer";

// Example of composite formatting
Console.WriteLine("Hello, my name is {0} {1} {2}. I am {3} years old. I am a {4}.", fname, mname, lname, age, job);
Output of composite formatting

You can see that composite formatting also gives the output that was needed. The potential issues are readability and bugs while writing the code. Reading this type of code can be slow because you need to glance back and forth to check which variable is positioned to be displayed in which placeholder. It is also prone to errors because it can be easy to mix up the placeholder and the matching variable.

String Interpolation

We have now made it to string interpolation. An interpolated string begins with the dollar sign ($) character. There can be no space between the dollar sign ($) and the starting double quote (“) of the string. Variables are placed directly in curly braces in the string and their values are displayed when the application is ran. The below code shows an example of string interpolation.

string fname = "Jessie";
string mname = "Andrew";
string lname = "Edmisten";
int age = 39;
string job = "Software Developer";

// Example of string interpolation
Console.WriteLine($"Hello, my name is {fname} {mname} {lname}. I am {age} years old. I am a {job}.");
Output of string interpolation

From the example, it is easy to see the benefits of using string interpolation. The string is much easier to read because we can glance at it and see where the variables will be in the string during runtime. There is no need to glance back and forth from a section of the string to the argument list. Writing the code is also less likely to have errors because it is shorter, there is not a separated argument list, and it less common to miss spaces or periods. If a mistake is made it will be quicker to find and update.

To illustrate the convenience of string interpolation compared to concatenation and composite formatting the code below shows all three examples next to each other.

string fname = "Jessie";
string mname = "Andrew";
string lname = "Edmisten";
int age = 39;
string job = "Software Developer";

// Compare concatenation, composite formatting, and interpolation
Console.WriteLine("Hello, my name is " + fname + " " + mname + " " + lname + ". I am " + age + " years old. I am a " + job + ".");
Console.WriteLine("Hello, my name is {0} {1} {2}. I am {3} years old. I am a {4}.", fname, mname, lname, age, job);
Console.WriteLine($"Hello, my name is {fname} {mname} {lname}. I am {age} years old. I am a {job}.");
Output from concatenation, composite formatting, and interpolation

References:
https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
https://www.youtube.com/watch?v=6hESuj1rtvc

Leave a Comment

Your email address will not be published. Required fields are marked *