Code contracts build reference assembly actions

The Contract Reference Assembly is a special kind of assembly which preserves any code contracts you defined in your source code files. This is necessary because at compile-time, the code contracts’ “rewriter” (ccrewriter) removes or replaces each contract with equivalent verification code (Contract.Requires(someBool) might be rewritten as if (!someBool) throw). Without the code contracts, if … Read more

Should the Code Contracts static checker be able to check arithmetic bound?

I’ve had an answer on the MSDN forum. It turns out I was very nearly there. Basically the static checker works better if you split out “and-ed” contracts. So, if we change the code to this: public static int RollDice(Random rng) { Contract.Ensures(Contract.Result<int>() >= 2); Contract.Ensures(Contract.Result<int>() <= 12); if (rng == null) { rng = … Read more

What is a practical usage of Code Contracts in .NET 4.0?

From The Code Contracts User Manual: Contracts allow you to express preconditions, postconditions and object invariants in your code for runtime checking, static analysis, and documentation. Code Contracts are used for static verification; imagine if – at compile time – you caught not only syntax errors but also logic errors. This is the vision of … Read more

False positive: precondition is redundant

I know this doesn’t directly answer your question, but it appears you’re using a legacy mode for Code Contracts. This document describes the recommended assembly mode based on usage requirements: http://research.microsoft.com/en-us/projects/contracts/userdoc.pdf From Pages 20, 21… Another snippet from the document: 5.1.1 Assembly Mode The contract tools need to know which usage mode you choose. If … Read more