如何处理 C# 字符串插值中的空值?

How are null values in C# string interpolation handled?

在 C# 6.0 中,添加了字符串插值。

string myString = $"Value is {someValue}";

上面的例子是如何处理空值的? (如果 someValue 为空)

编辑: 只是为了澄清,我已经测试并知道它没有失败,这个问题被打开以确定是否有任何需要注意的情况,在使用字符串插值之前我必须检查空值。

这与 string.Format("Value is {0}", someValue) 相同,后者将检查 null 引用并将其替换为空字符串。但是,如果您像这样 string.Format("Value is {0}", null) 实际传递 null,它会抛出异常。但是,在 $"Value is {null}" 的情况下, null 首先设置为参数并且不会抛出。

来自TryRoslyn,反编译为;

string arg = null;
string.Format("Value is {0}", arg);

String.Format 将为 null 值使用空字符串。在 The Format method in brief 部分;

If the value of the argument is null, the format item is replaced with String.Empty.