The String Interpolation ($) Special Character
While developing applications, it is quite common that you are required to produce formatted output. This is a common scenario for any developer whether you develop console application, web application or Mobile application. With C# 6 onwards Microsoft introduced a special character called interpolation operator ($) to help developers easily manipulate the string literals.
In this article, I am briefly explaining the interpolation operator with the help of couple of examples.
Let us consider the example of adding two numbers and print their sum as the output. Very simple example. The below is the old (ugly) way of doing this in C# with ASP.Net.
int a = 2, b = 3, sum = a + b;
Response.Write("The sum of " + a + " and " + b + " is " + sum);
This code produces the following output
The sum of 2 and 3 is 5
I am sure most of you will not agree with this code as the string concatenation is always a pain and there are better alternatives. Let us organize the code by using String.Format
int a = 2, b = 3, sum = a + b;
Response.Write(String.Format("The sum of {0} and {1} is {2}", a, b, sum));
This was the preferred method for most of the developers out there (before C# 6). And it produces the exact same output as the previous statement.
I am sure this is also painful when dealing with relatively large string and with more number of parameters. The ordering of parameters and using it in the code block always steal some time from the developers. The interpolation special character ($) helps the developers to write the code in a better way.
Consider the following example.
int a = 2, b = 3, sum = a + b;
Response.Write($"The sum of {a} and {b} is {sum}");
This code also produces the exact same results as the previous two. You will notice how easy and readable the code is. The $ symbol make the string an interpolated string. When it is resolved, the expressions inside the braces ({}) are replaced by the corresponding expression results. Cool!.
The following example.
Response.Write($"Today is {DateTime.Now.ToShortDateString()}");
Will produce the result
Today is 8/10/2019
You can combine both $ and @ operator together in a string. See the below example
String Username = "Mark Smith";
Decimal amount = 89.560m;
Response.Write($@"<pre>Dear {Username},
Your Total Due for this month is {amount}.
Please settle the amount at the earliest.
</pre>");
This statement produces the following output in the browser.
Also it is possible to specify the minimum number of spaces the string should hold. By default the text is right aligned, the negative value for the alignment parameter will make it left aligned.
See the following example
Response.Write($@"<pre>
9 x 1 = {9 * 1,2}
9 x 2 = {9 * 2,2}
9 x 3 = {9 * 3,2}
</pre>");
The output for this will be
See the alignment of the results (9,18,27), it is right aligned. If you use -2, it will be left aligned.
You can also format the expression by using the format expression. Consider the following example
Response.Write($"Today is {DateTime.Now:dddd dd MMMM yyyy}");
This will produce the following result
Summary
The interpolation special character will help the developers to manipulate the string in more easy and flexible manner.