Download presentation
Presentation is loading. Please wait.
Published byHugo Welch Modified over 9 years ago
1
Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1
2
Specifying Object Invariants Annotate a method [ContractInvariantMethod] Inside the method you specify the object invariants [ContractInvariantMethod] private void ObjectInvariant() { Contract.Invariant(Salary >= 0); // Salary is a property Contract.Invariant(!String.IsNullOrEmpty(Name)); } If you write “hard” object invariants you may have to make an explicit constructor to initialize properties to legal values In case the default values are not legal Microsoft Code Contracts2
3
The class Contract Specifying pre- and post-conditions Namespace System.Diagnostics.Contracts http://msdn.microsoft.com/en- us/library/system.diagnostics.contracts.contract(v=vs.110).aspx http://msdn.microsoft.com/en- us/library/system.diagnostics.contracts.contract(v=vs.110).aspx Some interesting static methods Requires(Boolean expression) Used to specify pre-condition Comes in 4 variations With / without message With / without specific exception The default exception is ContractException. This exception is not public, so you cannot catch it! Normal exceptions are handy when you Unit test Ensures(Boolean expression) Used to specify post-condition Comes in 4 variations … (like Requires) EnsuresOnThrow (…) Microsoft Code Contracts3
4
Static vs. run-time analysis Static analysis Conditions and invariants are checked at compile-time Violations shown in Visual Studio (underlining) Run-time analysis Conditions and invariants are checked at run-time. Violations reported as messages or exceptions Microsoft Code Contracts4
5
Configuration: Plugin needed Code Contracts can be configured in many ways To do this you need a plugin for Visual Studio Code Contracts for.NET http://visualstudiogallery. msdn.microsoft.com/1ec 7db13-3363-46c9-851f- 1ce455f66970 http://visualstudiogallery. msdn.microsoft.com/1ec 7db13-3363-46c9-851f- 1ce455f66970 In Visual studio right click a project and you’ll see an extra menu item “Code Contracts” Microsoft Code Contracts5
6
References and further readings Nagel et al. Professional C# 5.0 and.NET 4.5.1, Wrox 2014 Code Contracts, page 522-528 Microsoft Research: Code Contracts http://research.microsoft.com/en-us/projects/contracts/ MSDN Code Contracts http://msdn.microsoft.com/en-us/library/dd264808(v=vs.110).aspx MSDN Contract Class http://msdn.microsoft.com/en- us/library/system.diagnostics.contracts.contract(v=vs.110).aspx http://msdn.microsoft.com/en- us/library/system.diagnostics.contracts.contract(v=vs.110).aspx Code Contracts for.NET Plugin for Visual Studio http://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f- 1ce455f66970 http://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f- 1ce455f66970 Microsoft Code Contracts6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.