In Code Contracts, Preconditions are specified using Contract.Requires to specify constraints on the input to a method. Postconditions are used to specify express constraints on the output of a method using Contract.Ensures. Here is a code snippet from C# in Depth, Chapter 15, which shows code contract in action
static int CountWhitespace(string text)
{
Contract.Requires(text != null, "text");
Contract.Ensures(Contract.Result() >= 0);
return text.Count(char.IsWhiteSpace);
}
No comments:
Post a Comment