如何为IFormattableString赋值?($“hi{name}”在c#6.0新的IFormattablestring特性中)
如何为IFormattableString分配值 不可能按如下方式使用吗如何为IFormattableString赋值?($“hi{name}”在c#6.0新的IFormattablestring特性中),c#,c#-6.0,C#,C# 6.0,如何为IFormattableString分配值 不可能按如下方式使用吗 string name = "Booley"; string template = "Hi mr. {name}, hope you enjoy the chicken!"; Console.WriteLine(template,name); 输出 Hi mr. Booley, hope you enjoy the chicken! 我知道如何实现这一点,但我很好奇为什么不能使用新的C#6.0特性来实现这一点。变量名
string name = "Booley";
string template = "Hi mr. {name}, hope you enjoy the chicken!";
Console.WriteLine(template,name);
输出
Hi mr. Booley, hope you enjoy the chicken!
我知道如何实现这一点,但我很好奇为什么不能使用新的C#6.0特性来实现这一点。变量名仅在编译时可用?字符串前面缺少$。它应该是
stringtemplate=$“你好,{name}先生,希望你喜欢鸡肉!”代码>我不明白这个问题。您谈论的是IFormattableString
,但在代码中没有使用它。你到底想做什么?这就是重点,我不想使用$,我想从文件中加载字符串。@hrein1000这个注释没有意义。您是否需要字符串插值(以$
开头的字符串)。字符串插值是一种语言功能,由编译器实现,而不是格式化字符串的新语法。运行时在哪里可以找到从文件加载的字符串中指定的变量名?IL不存储变量名。