Creating code contract Result method

Sometimes, we simply want a way to validate the result of a method. We want to be able to check what is returned and validate it against some or the other condition. It is here that the code contract Result can be used. It will inspect the value returned by the method under contract against the contract specified, and then it will succeed or fail.

How to do it…

  1. Before you go on, ensure that you have added the code contracts using statement to the top of your Recipes.cs class file:
    using System.Diagnostics.Contracts;
  2. In the Recipes class, add a new method called ValidateResult() that takes two integer values as parameters:
    public static int ValidateResult(int value1, int value2)
    {
        
    }
  3. To this method, add the code contract ...

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.