String templating with dollar sign

There are many different ways to construct a string in C#. One of the most convenient way is to use the dollar sign ($). As shown in this example:

string stringVar = "myStringVar";
int intVar = 2022;
double doubleVar = 2022.22;
object objectVar = new {  objectStringVar = "objectStringVar" , objectIntVar = 22 };

string templatedString = $"This is a stringVar: {stringVar}, " +
    $"that one is an intVar: {intVar}, " +
    $"here comes doubleVar: {doubleVar} , " +
    $"finally the objectVar: {objectVar}";

Console.WriteLine(templatedString);
Console.ReadLine();

This one will produce the following result:

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.