with Static Methods for DbC It is a spin-off from the learnings of the Spec# project Obtaining and Installing Academic and Commercial Licenses Tools Provided Integration with Visual Studio 3
Precondition ◦ Condition that is checked on entry to method Postcondition ◦ Condition that is checked on exit of method Invariant (constrain state of objects) ◦ Condition checked on exit of constructor and all public methods and properties Static (Compile-time) and Runtime checking 4
Base Class Library (BCL) No additional references required .NET 3.5 or earlier Separate assembly Microsoft.Contracts.dll Need to add reference to assembly in your projects Contract classes contains in: System.Diagnostics.Contracts namespace Code Snippets for Visual Studio e.g. ci [TAB][TAB] => Contract.Requires(…) 6
Contract Inheritance Supports Abstract Methods and Interfaces Generate API documentation Hooks into XML documentation and inserts contract requirements (requires, ensures) 8
Only public surface contracts Call-site requires Custom rewriter methods Assembly rewriting via ccrewrite.exe Inserts code in project assemblies to enforce contracts at runtime 9
Provides static and runtime checking of: Preconditions Postconditions Object invariants Extends generated XML documentation Some known issues (still work in progress) Build slowdown - will be addressed in future Closures - static checking does not work for closures Edit-Continue does not work with code contract rewriting on No contracts allowed on delegates No contracts on iterators that use “yield” as IL the code changed into different form (workaround exists for this) 13