.NET – Why Object Reference Error Exceptions Don’t Specify Null Object?

.net-4.0debuggingnullreferenceexception

Maybe asking the question betrays my lack of knowledge about the process, but then again, there's no better reason to ask!

Tracking these down can be frustrating because stack traces can help me know where to start looking but not which object was null.

What is going on under the hood here? Is it because the variable names aren't bundled in the executable?

Best Answer

.NET code built with full optimizations and no debug info: your local variable names are gone, some local variables may have been eliminated entirely.

.NET code built with full optimizations + PDB (or full debug): most local variable names preserved, some local variables may have been eliminated

No optimizations + no debug info: local variable names are gone.

And then we have to consider that whatever you're dealing with may not be in a local variable at all - it might have been the result of a previous function call, on which you're chaining a new function call.