Questions tagged [code-contracts]

Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs.

Contracts is Microsoft's reference implementation of the "Design by contract" methodology. According to Microsoft Research, "Code Contracts bring the advantages of design-by-contract programming to all .NET programming languages." They can be checked statically at compile time or dynamically at run time, or be used to create unit tests semi-automatically.

The bulk of its functionality is implemented as a set of static library methods for writing pre-conditions, post-conditions and invariants; this allows all .NET languages to write contracts without any special extensions. All contracts are expressed as static calls to the methods Contract.Requires and Contract.Ensures. Therefore, normal utilities of the .NET tool chain such as Intellisense and compile-time checking can be applied to the task of writing contracts.

Like all language or library tags, this tag should be used for questions about issues in using this tool for writing code, or about its suitability for a particular purpose.

13 questions
27
votes
4 answers

Why would I use code contracts

I recently stumbled upon Microsoft's framework for code contracts. I read a bit of documentation and found myself constantly asking: "Why would I ever want to do this, as it does not and often cannot perform a static analysis." Now, I have a kind of…
Falcon
  • 19,308
3
votes
4 answers

Why must the burden of proof rest with the caller and not the method of the class that is being called?

Michael Perry states in his Pluralsight course on Provable Code (transcript subscription only) that: [T]he burden of proof rests with the caller In a code contract, why must the burden of proof rest with the caller and not the method of the class…
Phil C
  • 1,966