C# – How to use string interpolation?

String interpolation is useful to create the formatted strings in a more readable form. C# uses the symbol “$” to represent the given string is an interpolated string. String interpolation allows us to use the variables within the string; and variables will be replaced with the actual values at the time of execution. This gives better control and readability of the code.

Through this article, we are going to discuss string interpolation in C#.

String interpolation allows to place the placeholders and these will be replaced with actual values when executing the program. In C#, the interpolated string must be prefixed with the symbol “$’ and the placeholders or variables must be enclosed with in the curly braces, “{}”. For example, below is an interpolated string;

double pi = 3.14159265359;
Console.WriteLine($"The value of the PI is {pi}.");

Once you run the above code in C#, it will display below output.

The value of the PI is 3.14159265359.

A placeholder in the interpolated string can be a variable or an expression. The expression will be evaluated and result will be replaced the placeholder. Here is another example, where the placeholder is an expression;

Console.WriteLine($"Pythagoras' constant (square root of 2) is {Math.Sqrt(2)}.");

After running the above statement; C# will show the below output.

Pythagoras' constant (square root of 2) is 1.4142135623731.

Using escape sequences in an interpolated string

We can use escape sequences as we use in the string literals. Escape sequences should be prefixed with a back slash (“\”). For example, “\n” for new line; “\t” for tab etc,.

But, we need to take special care when displaying curly braces; as these are used for interpolation expression, we must doubling them; in order to display them in the output. For example, to display “{“, we must doubling it as “{{“.  If you attempt to use it as escape sequence, “\{“; compiler will through the below error;

 error CS8087: A '{' character may only be escaped by doubling '{{' in an interpolated string.

Similarly, interpolation expression should be properly enclosed in curly braces “{}”. If we miss, compiler will through, below errors;

error CS8076: Missing close delimiter '}' for interpolated expression started with '{'.
error CS8086: A '}' character must be escaped (by doubling) in an interpolated string.

Here is the example, to display the curly braces in the output. Notice that, three curly braces are used to display the output in curly braces. Got it? You can get it, once you see the output of the below code;

string name = "Hubble";

Console.WriteLine($"Hello, {name}!");
Console.WriteLine($"Hello, {{{name}}}!");

You will see, below output; after the successful compilation of above code; notice that, one name is displayed in curly braces (“{}”);

Hello, Hubble!
Hello, {Hubble}!

Using verbatim interpolated string

C# uses the symbol “@” to represent the verbatim string literals. The symbol “@” has to be prefixed with string literals; to represent verbatim string literals. Verbatim string literals are useful to represent the string as it is defined.

When we use interpolated strings with verbatim strings; we have to always use the string interpolation symbol “$”, before the verbatim identifier symbol “@”. The sequence is important here; otherwise, C# will throw below error;

error CS8401: To use '@$' instead of '$@' for an interpolated verbatim string, please use language version 'preview' or greater.

Below is an example of using verbatim interpolated string;

string folder = "C#";
string path = $@"C:\Projects\{folder}";

Console.WriteLine(path);

Observe that, path variable is a verbatim interpolated string; represented with “$@” symbols.

(Raju)

C# – How to use string interpolation?

Leave a Reply

Your email address will not be published.

Scroll to top