I recently saw a post by Jeremy Miller which inspired me to create a new post category called Programming Paradigms. I've been doing .Net development for a while now, and even though by no means am I an expert in every facet of .Net development I figured I'd start posting some basics for any developers new to the framework (and stuff that can still bite seasoned developers in the bum). As such they will be relegated to the realm of the .Net world unless otherwise noted.
Probably the most common .Net runtime error is the pervasive null reference exception. Publicly accessible API's should check for nulls on all parameters when it makes sense to do so, and calling code should check for nulls returned from method calls. When nulls are encountered, have a backup plan in place, i.e. either assign a default value, or branch and take a different action. The bottom line is always test for nulls regardless if you think a null reference can actually be returned. Also, use so-called "safe casts" via the as keyword as cast failures will silently be converted to a null reference at runtime...you still must check for nulls after the safe cast and take appropriate action. The new coalescing operator (??) in C# 2.0 makes expressing the check-for-nulls-first paradigm more intuitive.
Mon, Jan 15 2007 11:41 PM