String Interpolation in C#.
Interpolated string in C#, is a string preceded by the $ symbol.
Such a string may contain special expressions in curly brackets:
int x = 18;
Console.Write($"Указанный возраст {x} лет."); // Выводит: Указанный возраст 18 лет.
Inside curly brackets can be any valid C# expression of any type. C# converts the value value to string, using the ToString() method or its equivalent for the selected type.
The output format can be changed by adding a formatting expression after the colon, for example:
string s = $"223 in HEX format: {223:X2}"; // X2 - Двухзначный шестнадцатиричный формат числа.
// Результат \\"223 in HEX format: DF\\"
Interpolated string must be a single line or contain a literal identifier "@", for example:
int x = 10;
string s = $@"The wall height is {
x} meters.";
To include a curly bracket in an interpolated string, simply repeat the curly bracket twice.
Comments (0)
No comments yet.