O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

NullReferenceException

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required