1. Better Contracts. Better Code. An Introduction to Microsoft Code Contracts in the .NET 4.0 Framework August 2010 W. Kevin Hazzard, C# MVP
2. What is code? Intent Assumption Binding Promise Guarantee Obligation http://www.asu.edu/clas/shs/aald/
3. A Simple Illustration public int[] ApplyDiscounts( intmaxPercentageAllowed, Customer[] customers, Promotion[][] promotions ) { // apply the promotions here // and return the results }
4. C# Does OK with Types & Structure Customer int int int public int[] ApplyDiscounts( intmaxPercentageAllowed, Customer[] customers, Promotion[][] promotions ) {} Customer int Customer Promotion Promotion Promotion Promotion Promotion Promotion
5. Questions Left Unanswered What is the acceptable range for the integer maxPercentageAllowed parameter? Can the customers or promotions arrays references be null or contain nulls? The promotions array is jagged. If an associated customer has no promotions, should the second-dimension array be null or empty? What range will the integers in the array result have?
6. Microsoft Code Contracts Static code verification Runtime checking, too Cross-language support Enhances documentation Works with .NET Fx 3.5; built into 4.0 Supports pre/post-conditions & invariants Helper classes for iteration, assertion, etc.
7. Samples Survey Let's take a look at the Code Contract samples which are excellent for learning how this stuff works.
8. Contract Reference Assemblies Something like "buddy classes" for contracts Contain visible interface and contracts but no code bodies ccrefgen.exe tool can be used to generate Some have been pre-built and ship with the Code Contracts installer Silverlight 3.0 and 4.0 .NET Framework 3.5 and 4.0
9. Patching Sandcastle Open the SandCastle zip file in the Code Contracts installation directory Copy the templates into your installed SandCastle folder Now help builds from XML comment documentation will include contract information A future release of SandCastle should remove the need for patching
10. Useful URLs MSDN Forum for Code Contractssocial.msdn.microsoft.com/Forums/en-IE/codecontracts Microsoft Code Contractsresearch.microsoft.com/en-us/projects/contracts Kevin's Blogdevjourney.com
12. Useful URLs MSDN Forum for Code Contractssocial.msdn.microsoft.com/Forums/en-IE/codecontracts Microsoft Code Contractsresearch.microsoft.com/en-us/projects/contracts Kevin's Blogdevjourney.com
Hinweis der Redaktion
What is the acceptable range for the integer maxPercentageAllowed parameter?Can the customers or promotions arrays references be null or contain nulls?The promotions array is jagged. If an associated customer has no promotions, should the second-dimension array be null or empty?What range will the integers in the array result have?