ToString() returns null?

倖福魔咒の 提交于 2019-12-07 16:53:56

问题


I am trying to determine cause of 'Null reference exception' on published remote site. So, I can't debug it directly and can operate only with logs. So my question is:
Is it possible, that .ToString() method of any of built-in .NET types returns null?

EDIT:
I suspect DateTime.ToString(invariantCulture) method with badly constructed culture settings.


回答1:


I don't know of any types where it does. It's not impossible - it would certainly be easy to write your own type which behaved like that - but I doubt any of the framework types do.

Do you have any particular types in mind?

EDIT: DateTime.ToString(invariantCulture) should never return null - the culture settings should be irrelevant, if you've really got the invariant culture.




回答2:


Correction, Object is not null if it returns null, NPE will thrown for null Object before toString() will be even tried to be invoked :)




回答3:


You can also override the ToString() method and assign it a custom value. If it returns null still, then the object is null.

http://msdn.microsoft.com/en-us/library/ms173154%28v=vs.80%29.aspx



来源:https://stackoverflow.com/questions/6815187/tostring-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!