Argument Validation

The book sample code commonly uses a custom ArgumentValidator class to ensure that method arguments are not null or fall within a valid range. This allows a method to fail fast, rather than continuing and raising a more difficult to diagnose error.

You frequently see statements like the following at the beginning of a method:

string PerformSomeAction(string value){    stringField = ArgumentValidator.AssertNotNull(value, "value");...}

Here, if value is null, then an ArgumentNullException is thrown. If not null, then the stringField field is set to the value in a fluent manner.

Note

Microsoft has a far more feature rich argument validation tool called Code Contracts, which integrates into Visual ...

Get Windows® Phone 8 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.