String Interpolation (C# 6)
String Interpolation (C# 6)
Section titled โString Interpolation (C# 6)โString interpolation lets you embed expressions inside strings with the $ prefix.
var name = "Ada";var score = 42;var msg = $"Hello {name}, your score is {score}.";// Hello Ada, your score is 42.
Notes
- Use alignment and format strings:
${score,6}
or${price:0.00}
. - Prefer interpolation over string.Format for readability.
Before vs After
Section titled โBefore vs AfterโBefore (string.Format):
var name = "Ada";var score = 42;var msg = string.Format("Hello {0}, your score is {1}.", name, score);
After (interpolation):
var name = "Ada";var score = 42;var msg = $"Hello {name}, your score is {score}.";