Creating code contract postconditions
Just as code contract preconditions control what information is passed to the method under contract, code contract postconditions control what information the method under contract returns to the calling code. You can, therefore, specify that the method will never return a null value or an empty dataset, for example. The actual condition does not matter; this is something that will change on a case-by-case basis. The important thing to remember here is that this code contract allows you to have more control over the data returned by your code.
Getting ready
Assume that the method under contract needs to ensure that the value returned will always be greater than zero. Using a code contract postcondition, we can easily enforce this rule.
How to do it…
Before you start, make sure that you have added the following
using
statement to the top of yourRecipes
class:using System.Diagnostics.Contracts;
In the
Recipes
class, add a method calledNeverReturnZero()
and...