Dav*_*vid -1 c# string-interpolation c#-6.0
是否可以使用您想要插入的字符串格式的变量.
public class Setting
{
public string Format { get; set; }
}
var setting = new Setting { Format = "The car is {colour}" };
var colour = "black";
var output = $"{setting.Format}";
Run Code Online (Sandbox Code Playgroud)
预期产出
"汽车是黑色的".
不,你不能这样做,但你可以通过稍微不同的方法来实现相同的目的,我喜欢这种方法:
public class Setting
{
public Func<string, string> Format { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后您可以将字符串参数传递给Format:
var setting = new Setting { Format = s => $"The car is {s}" };
var output = setting.Format("black");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1101 次 |
| 最近记录: |