I so often see numerous projects online that fail to validate input arguments in C#.  Back in the early days of C++ this was not only common, but often required by employers.  What happened?  Did we just become so lazy that we’d rather recieve a null reference exception or index out of range instead of actually checking input values? 

Reguardless of how we came to be here if your developing an API for external consumption it IS your responsibility to check arguments.  It really isn’t alot of work, and thus the reason I published a VERY small example of how to achieve this.  The code for Check.cs is published on code.google.com along with the test case.

 I use template methods on the various assertion routines (allowing type T to be inferred) so that I can use the value in the same line of code I make the assertion.  Instead of just passing a value, I simply pass the result of the assert call.  This allows code to look something like “this._myInfo = Check.NotNull(arginfo);”.  In this example I can set a member in the constructor at the same time validating that the argument was not null. 

Try it out and feel free to extend the class as your needs arise.

Comments