Creating code contract preconditions
Preconditions allow you to control exactly what the parameters need to look like before they are used in your method. This means that you can assume a lot of things about the data being sent to your method by the calling code. You can, for example, specify that a parameter should never be null or that a value must always be within a specific value range. Dates can be checked, and objects can be verified and vetted.
You have complete control over the data coming in to your method. It gives you the peace of mind to use that data once it has passed your contract without having to do additional checks.
Getting ready
Be sure that you have installed code contracts and that you have configured the settings correctly in the project properties, as described in the previous recipe.
How to do it…
In your
Recipes
class, create a new method calledValueGreaterThanZero()
and have it take an integer as a parameter:public static class Recipes { public static void ValueGreaterThanZero...