Languages
[Edit]
EN

C# / .NET - string interpolation

17 points
Created by:
Kate_C
19790

C# 6 introduced string interpolation. The main idea is similar to String.Format notation - it is just simplification and the difference is to put before string literal dolar $ sign and put each variable to curly brackets { } signs. Inside curly bracket used are same rules like in String.Format case but variable names are used instead of variable indexes put as parameters. This approach is more clear for programmers. 

String interpolation example

string name = "John";
int age = 45;
string bar = "---John---";

string text = $"My name is {name}. I am {age} y.o.\n\n{bar,29}";

Console.WriteLine(text);

Output:

My name is John. I am 45 y.o.

                   ---John---
Note: the text variable could be written as string text = String.Format("My name is {0}. I am {1} y.o.\n{2,20}", name, age, bar); according to previous approach. 

References

  1. String interpolation - Microsoft Docs
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join