$
特殊字符将字符串文本标识为内插字符串。 内插字符串类似于包含内插表达式的模板字符串。 将内插字符串解析为结果字符串时,带有内插表达式的项会替换为表达式结果的字符串表示形式。 此功能在 C# 6 及更高版本中提供。
扯那么多犊子还是来看看实际用法:
string 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.
备注
字符串开头的 $
和 "
之间不能有任何空格。 如果有空格,会导致编译时错误。
--------------------- 本文来自 stand_tough 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/weixin_40207730/article/details/80389388?utm_source=copy