#Defensive Programming

Imagine any .NET codebase you have worked on. What would be the most common usage of if statement in this code? Given the notion of The Billion Dollar Mistake, I bet it is the null check. Reference types in .NET are allocated on the managed heap, so when an instance of such a type is assigned to a variable, this variable essentially points to an adress in this managed heap. The default value of such a variable is null, meaning that it points to nothing and can’t be dereferenced. For instance, if you write a method with a reference type argument, you can’t always predict how this method is going to be invoked and there is no guarantee that it won’t be a null value. To protect your code from an unexpected NullReferenceException, you would typically write something like this: