Introduction to string interpolation
Tags: dotnet, csharp, beginners
Published: 2021-03-28 14:57:20


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](https://dev.to/carlhugom/creating-your-first-net-c-program-1poj), but that's not mandatory.

> This article is part of a sub-series, starting with [Introduction to string concatenation](https://www.forevolve.com/en/articles/2021/03/21/learn-coding-with-dot-net-core-part-6/).
> 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](https://www.google.com/search?q=interpolation&oq=interpolation)), 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:

```csharp
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:

```csharp
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:

```csharp
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);
```

Unfortunately, I was not able to recreate the exercise on this platform, so please look at the exercise on the original post [on my blog](https://www.forevolve.com/en/articles/2021/03/28/learn-coding-with-dot-net-core-part-7/#exercise). I'm sorry for the inconvenience.

## 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** which is coming soon. Stay tuned by following me on dev.to, [Twitter](https://twitter.com/CarlHugoM), or other places you can find me.
You can look at [my blog contact page](https://www.forevolve.com/contact/) for more info.