Introduction to string interpolation

A beginner guide to programming with .NET 5 and C#

Posted by Carl-Hugo Marcotte on March 28, 2021
Introduction to string interpolation
Photo by Jefferson Santos on Unsplash

In this article, we continue to explore string manipulations by focusing on interpolation. Instead of concatenating many pieces together, interpolation allows us to insert special tokens inside a string. A value then replaces those tokens. Interpolation and concatenation play the same role, but often one ends up being more elegant than the other and makes the code easier to maintain.

This article is part of a learn programming series where you need no prior knowledge of programming. If you want to learn how to program and want to learn it using .NET/C#, this is the right place. I suggest reading the whole series in order, starting with Creating your first .NET/C# program, but that’s not mandatory.

This article is part of a sub-series, starting with Introduction to string concatenation. It is not mandatory to read all articles in order, but I strongly recommend it, especially if you are a beginner. If you are already reading the whole series in order, please discard this word of advice.

Interpolation

The definition of interpolation, from Oxford Languages (Google search), is:

The insertion of something of a different nature into something else.

In C#, we must prefix the string with the $ character if we want to use interpolation. Then, we can insert the value of an expression in that string by wrapping it with { and }, like this:

var name = "Joe";
var result = $"Hello {name}!";
Console.WriteLine(result);

Executing the preceding code should write Hello Joe! in the terminal. I find $"Hello {name}!" to be more elegant than "Hello " + name + "!" and way easier to read, especially for a neophyte.

Next, we explore how to use interpolation in a multiline string.

Multiline string interpolation

If you wondered why I talked about multiline strings in the previous article, I planned this section. We can use both $ and @ to mix interpolation and multiline string, like this:

var name = "Joe";
var result = $@"Hello {name}!
What's up?";
Console.WriteLine(result);

In the preceding code, we used interpolation in a multiline string by chaining both $ and @ (in this order). As easy as that!

Note: the order is important. If you inverse the symbols order (@$"..." instead of $@"..."), the code will not compile.

Next, it is your turn to try it out!

Exercise

To practice interpolation, we will replace concatenation with interpolation in the code from the previous article’s exercise.

Here is the previous solution as a reference:

using System;

Console.Title = "IntroToDotNet";

Console.Write("What is your first name? ");
var firstName = Console.ReadLine();
Console.Clear();

Console.Write("What is your last name? ");
var lastName = Console.ReadLine();
Console.Clear();

var greetings = "Greetings " + firstName + " " + lastName + "!";
Console.WriteLine(greetings);

Here are a few optional hints in case you feel stuck:

Hint 1

You only have one line to update.

Hint 2

You only need to replace the following part of the code: "Greetings " + firstName + " " + lastName + "!".

Once you are done, you can compare with My Solution below.

My Solution

Program.cs

using System;

Console.Title = "IntroToDotNet";

Console.Write("What is your first name? ");
var firstName = Console.ReadLine();
Console.Clear();

Console.Write("What is your last name? ");
var lastName = Console.ReadLine();
Console.Clear();

// Only the following line changed
var greetings = $"Greetings {firstName} {lastName}!";
Console.WriteLine(greetings);

The only change is that I replaced the following line:

var greetings = "Greetings " + firstName + " " + lastName + "!";

By the following line:

var greetings = $"Greetings {firstName} {lastName}!";

Don’t you feel like interpolation is clearer than concatenation in this case? Well, if you don’t, I do, which is fine either way.

Good job! You completed another small chapter of your programming journey.

Conclusion

In this article, we explored interpolation as a way to replace concatenation in certain scenarios. At this point, using one or the other is only a matter of taste.

To use interpolation, we need to prefix a string with $. Inside that string, we can then wrap an expression, like a variable, with { and }. The program will replace that token at runtime with the expression’s value.

We also saw that we could use interpolation with multiline strings by prefixing the string with both special characters, like this: $@"...".

Next step

It is now time to move to the next article: Escaping characters in C# strings.

Table of content

Now that you are done with this article, please look at the series’ content.

Articles in this series
Creating your first .NET/C# program
In this article, we are creating a small console application using the .NET CLI to get started with .NET 5+ and C#.
Introduction to C# variables
In this article, we explore variables. What they are, how to create them, and how to use them. Variables are essential elements of a program, making it dynamic.
Introduction to C# constants
In this article, we explore constants. A constant is a special kind of variable.
Introduction to C# comments
In this article, we explore single-line and multiline comments.
How to read user inputs from a console
In this article, we explore how to retrieve simple user inputs from the console. This will help us make our programs more dynamic by interacting with the user.
Introduction to string concatenation
In this article, we dig deeper into strings and explore string concatenation.
Introduction to string interpolation You are here
In this article, we explore string interpolation as another way to compose a string.
Escaping characters in C# strings
In this article, we explore how to escape characters like quotes and how to write special character like tabs and new lines.
Introduction to Boolean algebra and logical operators
This article introduces the mathematical branch of algebra that evaluates the value of a condition to true or false.
Using if-else selection statements to write conditional code blocks
In this article, we explore how to write conditional code using Boolean algebra.
Using the switch selection statement to simplify conditional statements blocks
In this article, we explore how to simplify certain conditional blocks by introducing the switch statement.
Boolean algebra laws
This article explores multiple Boolean algebra laws in a programmer-oriented way, leaving the mathematic notation aside.
This is the end of this series
This article series was migrated to a newest version of .NET. Have a look at the .NET 6 series for more!




Comments