Interpolation of Strings in C# —The $-Special Character
Easily format strings with the interpolation
In C#, we can use the $ identifier before the beginning of a string to interpolate it with parameters. Here is an example:
Summary
The provided content discusses string interpolation in C# using the $ special character, which simplifies string formatting by embedding expressions directly within a string.
Abstract
The article explains how C# developers can leverage string interpolation to create more readable and maintainable code. It demonstrates the use of the $ identifier before a string to enable interpolation, allowing variables and expressions to be directly embedded and converted to strings. The syntax for interpolation includes optional alignment and formatting specifications, which can be used to control the output's appearance, such as padding and scientific notation for numbers. The author provides examples and references the official Microsoft documentation for a deeper dive into alignment and format string components. Additionally, the article encourages readers to support the author by joining Medium, subscribing to the newsletter, or becoming a member, which also benefits other writers on the platform.
Opinions
+ operator.In C#, we can use the $ identifier before the beginning of a string to interpolate it with parameters. Here is an example:
Since message is a variable, the expression above will replace the placeholder of the same name with its value, calling a .ToString() method if needed.
It is very clean and more human-readable than just appending pieces of strings around the variable using the + operator.
The interpolation offers also other useful tools. Let’s see.
The general syntax for the interpolation is this one
Where
expression can either be a string or a variable content to be interpolatedalignment is a negative (left) or positive (right) integer specifying the padding, it is introduced by a comma ,format is used in special cases where a format may be needed (decimal numbers, dates…), it is introduced by a colon :For example, the next interpolation will put the message with an alignment to the right, in particular, the message will appear as a string of length 12, with the padding (space chars) on the left and the content on the right.
For the format the functionality is similar. Here a float number will be put in a string using scientific notation.
If you want a deeper view of how to use alignment and format, here are links to the official documentation:
Check this to see how I put code in this article:
If you liked the article, please clap to it and share it! Also, take a look at my games!

Get access to my stories and those of other Medium writers for just $5 a month. With no additional cost to you, I will receive half of your payments as a commission: it’s a great way to support me!
Join my newsletter and receive regular notifications when I post.