C# in Depth

Cover of C# in Depth
Order now (3rd edition)

Nullity checking in comparisons and defensive coding

Chapter 1: The changing face of C# development: 1.1.2

Created: 1/22/2008
Last updated: 1/22/2008

In listing 1.4, the comparer doesn't check whether its parameters are null references, nor whether they're of the right type, nor whether the names of the products are null.

How much this matters in real life really depends on what you're doing. You can write defensive code everywhere, knowing that you'll get the most appropriate exception (or "null is less than everything" behaviour), or you can skimp and cope with the consequences. If you're the only one ever to use your types, you can be a bit more relaxed about parameter checking and the like, but when writing libraries for public consumption you should be a lot more careful.