string Interpolation - q'oshtirnoq ichida o'zgaruvchilardan foydalanish imkonini beradi, yana inject
(ukol qilish)
C# da Interpolation ichida, o'zgaruvchidan keyin son yozadigan bo'lsak o'sha son marta bo'sh joy qoldirib beradi.
agar musbat son bersak o'ng tomonga
agar manfiy son bersak chap tomonga
$
belgisi satr literalini interpolyatsiya qilingan qator sifatida belgilaydi. Interpolyatsiya qilingan satr - bu interpolatsiya ifodalarini o'z ichiga olishi mumkin bo'lgan satr harfidir. Interpolyatsiya qilingan satr natija qatoriga echilganda, kompilyator elementlarni interpolyatsiya ifodalari bilan ifoda natijalarining satr tasvirlari bilan almashtiradi.
String interpolyatsiyasi satrlarni formatlash uchun yanada o'qiladigan, qulay sintaksisni ta'minlaydi. String kompozit formatlashdan ko'ra o'qish osonroq.
var name = "Mark";
var date = DateTime.Now;
// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// Both calls produce the same output that is similar to:
// Hello, Mark! Today is Wednesday, it's 19:40 now.
Top comments (0)