FormattableString
自C# 6.0版本开始引入。我们可以使用string
对象来进行相同的字符串格式化,那么为什么还需要使用FormattableString
或IFormattable
呢?这三者有什么区别?
我的代码
var name = "Pravin";
var s = $"Hello, {name}";
System.IFormattable s1 = $"Hello, {name}";
System.FormattableString s2 = $"Hello, {name}";
首先,这会产生相同的结果,即'Hello Pravin'。
如果有人对此有更深入的了解,我可以得到更详细的答案吗?
string
未实现IFormattable
接口,因此当您将插值字符串转换为IFormattable
时,使用FormattableString
是唯一的选择。 - user4003407RawOuterXml = Invariant($"");
其中 RawOuterXml 和 xmlReader.Value 都是字符串。如果一个变量已经是字符串了,那么使用 Invariant 调用会有什么效果吗? - axeman