【发布时间】:2018-08-28 14:46:04
【问题描述】:
我们有一个名为“StringExtensions”的文件,它作为一个“类”工作,可用于我假设的多个数据集/模型。
谁能帮助理解ToSafeString 对字符串的作用?
public static string ToSafeString(this object source)
{
return source?.ToString() ?? string.Empty;
}
【问题讨论】:
-
如果你写像
object test = null;Console.WriteLine(test.ToSafeString());这样的代码,它可以防止NullReferenceException