visual studio tutorial - Pre and postconditions in Code Contracts - microsoft studio - microsoft visual



Pre and postconditions

using System.Diagnostics.Contracts;

public int IncrementByRandomAmount(int input)
{   
    Contract.Requires<ArgumentNullException>(input != null); // Don't allow null parameter.
    Contract.Requires<ArgumentOutOfRangeException>(input < int.MaxValue); // We can't do anything if we're given int.MaxValue.
    Contract.Ensures(Contract.Result<int>() > input); // Return value will be greater than input value.

    Random rnd = new Random();
    input += rnd.Next(1, 13); // Creates a number between 1 and 12 and adds it to input.

    return input;
}
click below button to copy the code. By Visual studio tutorial team

Related Searches to visual studio tutorial - Pre and postconditions in Code Contracts