For people coming from the native code land, NullReferenceException is your modern access violation counterpart in the managed world. If your programming background lies somewhere else, just remember this exception is something you never want to see in your code.

It’s too bad reference types by default permit the null reference, making this exception far too common in improperly tested code. Failure to return a proper object is often signaled by means of a null reference, giving callers a great opportunity to shoot themselves in the foot by omitting a null check. So, don’t fall into this trap yourself. Instead, check against null values wherever necessary:

string s = GetStringMaybeNull();string oops = s.ToUpper();

Another ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.