[英]How to handle null exception when converting from object to string?
只是好奇:
這些行拋出Invalid Cast Exception
: Unable to cast object of type 'System.Double' to type 'System.String'.
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;
我從library
收到obj
值。
當我們在枚舉時不知道object
的類型時,如何簡單地轉換為string
?
這就是為什么.net中的每個對象都具有ToString()方法(從Object繼承)的原因
string str = (obj == null) ? string.Empty : obj.ToString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.