Take the 2-minute tour ×

I'm trying to get VS2010 Ultimate with Code Contracts to generate Errors instead of Warnings.

I have this simple test program:

using System.Diagnostics.Contracts;

namespace MyError {

public class Program { static void Main(string[] args) { Program prog = new Program(); prog.Log(null); }

 public void Log(string msg)
     Contract.Requires(msg != null);

} }

It correctly determines there is a violation of the contract: C:...\Program.cs(10,13): warning : CodeContracts: requires is false: msg != null

In my csproj file there is this property field for Debug:


Is there something else I have to set in the project settings to turn these into errors?

share|improve this question
What's the reason for trying to enforce this, if I might ask? –  KoMet Dec 20 '10 at 16:11
i imagine its to make people write code that doesn't break the contracts. –  John Nicholas Jun 25 '11 at 11:17
add comment

3 Answers

It looks like at this point Microsoft has elected not to make this possible, but they are considering it for the future: http://connect.microsoft.com/VisualStudio/feedback/details/646880/code-contracts-dont-listen-to-treat-warnings-as-errors-setting

share|improve this answer
add comment

I was hoping to make any contract warnings stand out with an error or fail the build.

share|improve this answer
add comment

The problem is that the code contracts use a rewriter. they show as warnings because they are only calculated after the build completes.

Well i don;t really know how it works, but unless y ou built code contracts nito the copmiler i do not see how they could be anything but warnings / meswsages.

share|improve this answer
add comment

Your Answer


