CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C# – How to use string interpolation?

String interpolation is useful to create formatted strings in a more readable form. C# uses the symbol “$” to represent the given string as 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 within 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 the 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 the result will be replaced with 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 backslash (“\”). 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 double them; in order to display them in the output. For example, to display “{“, we must be doubling it as “{{“.  If you attempt to use it as an escape sequence, “\{“; the compiler will through the below error;

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

Similarly, interpolation expressions should be properly enclosed in curly braces “{}”. If we miss, the compiler will through, the 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 an 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, the below output; after the successful compilation of the 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 the 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 a verbatim interpolated string;

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

Console.WriteLine(path);

Observe that, the 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. Required fields are marked *

Scroll to top