问题
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