5

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.

Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153
  • 1
    It's more likely that the object you're calling .ToString() on is null. Do you have the possibility to view the stacktrace, and find the relevant piece of code? – J. Steen Jul 25 '11 at 11:14
  • If it's using the CultureInfo.InvariantCulture, it shouldn't fail. If it's badly formed CultureInfo, it'll give a CultureNotFoundException. – J. Steen Jul 25 '11 at 11:33

2 Answers2

7

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Since all(?) framework types (and all types) inherit from System.Object, and the Object.ToString() method simply returns the type name... wouldn't it actually be impossible? Unless, of course, someone's overridden ToString() to return null in some bizarre case... – J. Steen Jul 25 '11 at 11:18
  • @J. Steen: Someone overriding `ToString` in a way which returns null in some situations is exactly what I was considering. – Jon Skeet Jul 25 '11 at 11:52
-2

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

Lloyd
  • 29,197
  • 4
  • 84
  • 98
Jeffrey Kevin Pry
  • 3,266
  • 3
  • 35
  • 67
  • 4
    No method can be called on a null object. If you try to call `ToString()` on a null object it will throw a `NullReferenceException`. The conclusion “if it returns null still then the object is null” is not possible and thus **wrong**. The provided link seems to be dead and thus is not helpful for original context. – Kissaki Jun 29 '20 at 13:03
  • He asked if any object in .NET can return null. I answered saying if he overrides it and returns null in the string, it returns null. Sorry if I wasn’t clear, but it most certainly isn’t wrong. OP didn’t ask if calling ToString on a null throws an exception, he asked can it return null. – Jeffrey Kevin Pry Jun 29 '20 at 22:29
  • 1
    Also after 9 years links are likely to be stale when posts are resurrected from the grave. – Jeffrey Kevin Pry Jun 29 '20 at 22:36
  • 1
    That’s why StackOverflow guidelines suggest making answers self-sufficient, and replicating the relevant parts of the linked to content. The link in this answer is neither labeled nor given context on what it is for/attempts to reference or explain specifically. – Kissaki Jul 10 '20 at 08:10