Languages
[Edit]
EN

C# / .NET - string interpolation

17 points
Created by:
Kate_C
22870

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
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join