String Interpolation ($) feature introduced in C# 6.0, allows embedded expression in a string . The syntax prefixes a string literal with a dollar $ operator symbol and then embeds the expressions with curly braces ({}).
Lets look at an example below,
1 2 3 4 |
string firstName = "Raul"; string lastName = "Martinez"; //Used $ operator and expressions in curly braces Console.WriteLine($"Full Name : {firstName} {lastName}"); //prints "Full Name : Raul Martinez" |
In the above example, we have used the $ operator and variable expressions as firstName and lastName . When we run above sample then it prints “Full Name : Raul Martinez” .
String Interpolation & String.Format() :
The $ operator in C# or String Interpolation is an extended version of the String.Format() feature . However, it allows a more readable and convenient way of combining formatted string expression results into a string literal.
We can rewrite above string interpolation example by using String.Format() method as follows.
1 2 3 |
string firstName = "Raul"; string lastName = "Martinez"; Console.WriteLine(string.Format("Full Name : {0} {1}", firstName, lastName)); //prints "Full Name : Raul Martinez" |
While using String.Format() , here we insert the variable values in a string using index numbers like {0}, {1}, etc.
However, string interpolation allow us to directly include valid C# expression in side a string literal using braces like {expression}. Console.WriteLine($"Full Name : {firstName} {lastName}")
Formatting using String Interpolation :
We can also format the object using control formatting syntax using string interpolation expression. It uses below syntax for formatting an object.
${variable expression : formatting expression}
- variable expression : The string variable expression.
- formatting expression : It is the formatting expression of the result string output.
For example, in below code we have applied formatting information as {enrollmentDate:D} for the enrollmentDate variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; namespace StringInterpolationDemo { class Program { static void Main(string[] args) { string firstName = "Raul"; string lastName = "Martinez"; DateTime enrollmentDate = new DateTime(2020, 04, 12); //Output will be Full Name : Raul Martinez , Enrollment Date : 12 April 2020 Console.WriteLine($"Full Name : {firstName} {lastName} ,Enrollment Date : {enrollmentDate:D}"); } } } |
Alignment using String Interpolation :
Along with formatting the object value, we can also do alignment and add spacing to it. You can define the width of the string that is inserted into the result string by using syntax such as {enrollmentDate, 10} , it will inserts 10-character string in to the result string value.
It uses below syntax for adding spaces to object.
${variable expression , alignment value}
- alignment value : A signed integer value, that defines the width of string that is going to be inserted to the result string.
- To left-align strings in a field, you have to provide negative value, such as {enrollmentDate, -10}
- To right-align string in a field, you have to provide a positive value, such as {enrollmentDate, 10}
We can also include formatting string along with the alignment value to the expression as follows.
${variable expression , alignment value : formatting expression}
Lets look at below example to understand how this alignment and formatting expression works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; namespace StringInterpolationDemo { class Program { static void Main(string[] args) { DateTime date = new DateTime(2020, 10, 28, 6, 0, 0); decimal[] tempArr = { 70.452m, 65.98m, 71.6m, 74.1m, 72.156m, 72.228m }; DateTime tempDate; decimal temp; Console.WriteLine("Left Alighnment"); Console.WriteLine("*****************************************"); Console.WriteLine($"{"Date",-20} {"Temperature",-15}\n"); for (int i = 0; i < tempArr.Length; i++) { tempDate = date.AddDays(i); temp = tempArr[i]; Console.WriteLine($"{tempDate,-20:g} {temp,-15:N1}"); } Console.WriteLine("\n"); Console.WriteLine("Right Alighnment"); Console.WriteLine("*****************************************"); Console.WriteLine($"{"Date",20} { "Temperature",15}\n"); for (int j = 0; j < tempArr.Length; j++) { tempDate = date.AddDays(j); temp = tempArr[j]; Console.WriteLine($"{tempDate,20:g} {temp,15:N1}"); } } } } |
When we run above program then it generates below output.
If you look at the output, then you can find that we have implemented.
- Left alignment by using $”{“Date”, -20} {“Temperature”, -15} for both first and second argument.
- Right alignment by using $”{“Date”, 20} { “Temperature”, 15} for both arguments.
- We have used the formatting expression $”{tempDate,-20 : g} {temp, -15 : N1}” and $”{tempDate,20 : g} {temp, 15 : N1} to format the result string.
Excellent post, found similar post, if anyone want to read more about string interpolation here is the link
String Interpolation in C# with examples
Found it, riiiiiiiigh. That’s why you are listed as the author. 😛