Friday, May 19, 2017

Code Contracts

Ever want an common way to do a null parameter check or that an integer is positive, etc. If so, you may find MS Code Contracts useful. The downside is that all your files then have a dependency on this assembly. The upside is they are available in the System.Diagnostics.Contracts namespace which is part of the mscorlib.dll assembly so it should always be available.

Code Contracts provide a way to specify preconditions, postconditions, and object invariants in your code. Preconditions are requirements that must be met when entering a method or property. Postconditions describe expectations at the time the method or property code exits. Object invariants describe the expected state for a class that is in a good state.

The key benefits of code contracts include the following:
  • Improved testing: Code contracts provide static contract verification, runtime checking, and documentation generation.
  • Automatic testing tools: You can use code contracts to generate more meaningful unit tests by filtering out meaningless test arguments that do not satisfy preconditions.
  • Static verification: The static checker can decide whether there are any contract violations without running the program. It checks for implicit contracts, such as null dereferences and array bounds, and explicit contracts.

1 comment:

Philips Huges said...


Wonderful blog.. Thanks for sharing informative Post. Its very useful to me.

Installment loans
Payday loans
Title loans