You add preconditions contracts to tell the compiler that the code can be executed only if it respects the specified contract. Generally preconditions are useful replacements for custom parameters validation rules. For example, consider the following simple method that multiplies two numbers:


Inside the method body, the code checks for valid parameters; otherwise, it throws an exception. This is common, but code contracts provide a good way, too. The preceding method could be rewritten with code contracts as follows:


So you just invoke ...

Get Visual Basic® 2010 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.