从对象转换为字符串时如何处理null异常?

now*_*ed. 1 .net c# null

只是好奇:

这些线投掷Invalid Cast Exception: Unable to cast object of type 'System.Double' to type 'System.String'.

 Object obj = new object();
 obj = 20.09089;
 string value = (string)obj;
Run Code Online (Sandbox Code Playgroud)

obj从a 获得了价值library.

如何简单转换为string我们不知道object枚举时的类型?

ymz*_*ymz 5

这就是.net中的每个对象都有ToString()方法(继承自Object)的原因

string str = (obj == null) ? string.Empty : obj.ToString();
Run Code Online (Sandbox Code Playgroud)