Languages
[Edit]
EN

C# / .NET - string interpolation

17 points
Created by:
Kate_C
2854

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
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.