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 ...

Get C# Programming Cookbook 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.