Code Contracts
Discuss and provide feedback on Code Contracts.
Announcements
Welcome to Code Contracts for .NET
Mike BarnettMicrosoft EmployeeFriday, February 20, 2009 2:28 AMWe are so happy that you are interested in Code Contracts! You are at exactly the right place for learning how to use contracts from any .NET language to:
- Specify method preconditions for expressing conditions that a caller must meet in order to call a method.
- Specify method postconditions for expressing conditions that the implementation of a method must meet before returning. Conditions can be specified for both normal termination as well as exceptional termination.
- Specify object invariants that describe the healthy states for an instance of a class.
We currently provide three tools for making use of contracts:
- A binary rewriter that operates as a post-build step to inject the contracts into the appropriate places for them to be evaluated at runtime.
- A static checker that operates as a post-build step to analyze your code without even executing it for potential contract violations.
- A documentation generator that adds information about the contracts to the XML documentation files for your project.
The installer for these tools is available for either commercial or non-commercial use.
We welcome you to use the forum to ask questions about how to use contracts and the tools, to make suggestions for how we can improve, and --- if you really must --- to report bugs and problems.
You can also check out our home page at Microsoft Research.Release 1.4.40314.1 is now live!
Mike BarnettMicrosoft EmployeeWednesday, March 16, 2011 6:11 PMA new release of Code Contracts is now live. It includes just a couple of quick bug fixes for things we found in the previous release. Check out the release notes for the details.
If the download links have the usual caching problem, then you can try these:
Download Standard Edition CodeContracts
Download Premium Edition CodeContracts
Please keep using the forum for communicating with us. We are trying to make the case for Code Contracts to be included in Visual Studio as an official product. Please send us your feedback as soon as possible, especially if you have any details about how exactly it has helped your development process.
A new version of the Editor Extensions will be released this week. You may experience problems using the old (current) extensions at the same time as the new release. If you do see a problem with that, please let us know (and disable the editor extensions until the new release is ready).
New Release for Visual Studio Editor Extensions for C# (v1.4.31130.0)
Mike BarnettMicrosoft EmployeeThursday, December 09, 2010 10:24 PMThere is a new release of the VS 2010 editor extensions on the VS Gallery. The extensions themselves haven't changed, but the underlying infrastructure has had many contract-related improvements so I hope it performs better. I will try to get around to fixing the performance problems that everyone has been noticing.
If you haven't tried it yet, then please do and let us know what you think!
To review, the extensions should show you method contracts in the following situations:
- While you are typing a call to a method: the signature helper window should show you each overload's contract in the same window as the signature and exception information.
- Hovering over an existing method call: the quick info window should show you the method's contract in addition to its summary information.
- Method override and interface implementation definitions: a small graphic should pop up above the method definition showing any inherited contracts.
- Metadata windows: if you "go to definition" for a type/method that is defined in an external assembly then the metadata window that VS creates should be augmented with method contracts.
Documentation is available at the MSR Code Contracts site.
Filtering and SortingUse these options to narrow down the question and discussion list.
- 537548
Missing Contracts on Libraries
Manuel FahndrichMicrosoft EmployeeTuesday, March 10, 2009 6:20 AM - 2124
Invariant method doesn't help with Contract.Ensures using Nullable<long>
Sebastian Good Sunday, May 08, 2011 4:12 AM - 029
Bug: static checker doesn't recognize instance modification
Alexey R. _ 22 hours 21 minutes ago - 2473
Object disposal warning from Contract.Ensures()
Burt.Harris Thursday, March 03, 2011 5:39 PM - 1144
Can't install 1.4.40314.1
tetsu1 Wednesday, May 04, 2011 7:25 PM - 1178
Calling private method breaks constructor invariant check
JPGrk Tuesday, May 03, 2011 1:41 PM - 5255
Unbelievable Bug: Contract involving inheritance fails to verify when method name is 'Pack'
Strilanc Friday, April 22, 2011 3:17 PM - 2213
Suggestion: 'unnecessary suppression' should be warnings (if redundant assumptions is checked)
Strilanc Saturday, April 23, 2011 4:42 AM - 3917
Bug with code contracts in TestClass constructor?
Pando Wednesday, January 05, 2011 4:28 AM - 0197
ContractAbbreviatorAttribute Not Working
Taffarel Saturday, April 30, 2011 4:15 AM - 4228
Invariant on derived class not verified unless invariant exists on base
Martin Liversage Thursday, April 28, 2011 7:35 AM - 1210
Suggestion: add ContractStaticIgnored attribute (as a complement to ContractRuntimeIgnored)
Alexey R. _ Tuesday, April 26, 2011 6:35 AM - 2182
[BUG] Static code analysis gives unwarrented warning
Matthew WatsonMicrosoft Community ContributorWednesday, April 27, 2011 12:45 PM - 3511
Bug in container analysis
Alex Petersen Saturday, March 05, 2011 12:25 PM - 6753
PureAttribute on structs
Alex Petersen Thursday, March 03, 2011 8:16 PM - 1214
'Implicit non-null obligatoins' don't check a SafeHandle returned from a PInvoke call
Alexey R. _ Saturday, April 23, 2011 12:26 PM - 0139
Throw statement should be checked by the 'Implicit non-null obligations'
Alexey R. _ Saturday, April 23, 2011 11:49 AM - 0132
Bug: Suppressing a warning message doesn't suppress the associated 'suggested precondition' notice
Strilanc Saturday, April 23, 2011 2:38 AM - 4206
Can we turn off code contracts during compilation?
Mike Paterson Friday, April 22, 2011 12:52 AM - 1147
Bug: Verifier thinks an enum constant is not defined
Strilanc Friday, April 22, 2011 4:48 PM