我正在阅读代码合同,乍一看似乎是相当革命性的,但我似乎无法让它们工作。我正在运行 Windows 8 和 Visual Studio 2012 Premium(两者的发行版)。然后,我通过单击下载代码合同链接从此处安装了代码合同。
然后我在一个全新的控制台应用程序中编写了以下代码:
class Program
{
static void Main(string[] args)
{
var answer = Add(0, 5);
Console.Write(answer);
Console.ReadLine();
}
static int Add(int x, int y)
{
Contract.Requires(x > 0 && y > 0);
return x + y;
}
}
我预计编译会失败,因为 的第一个参数Add
是 0,但程序成功并将 5 打印到控制台。
我已经尝试过使用默认的代码合同设置,但也尝试了一些无济于事的东西。我当前的设置如下所示:
任何想法我做错了什么?
更新:
这是构建窗口的结果。它似乎正在做某事,但只是发出警告而不是错误。在我观看的视频中,这些东西被标记为编译错误,程序甚至无法运行。
1>------ Build started: Project: DeleteMe, Configuration: Debug Any CPU ------
1> DeleteMe -> c:\users\mike\documents\visual studio 2012\Projects\DeleteMe\DeleteMe\bin\Debug\DeleteMe.exe
1> CodeContracts: Task manager is unavailable.
1> CodeContracts: DeleteMe: Run static contract analysis.
1> CodeContracts: Suggested requires: Contract.Requires(false);
1> CodeContracts: DeleteMe: Validated: 0.0 %
1> CodeContracts: DeleteMe: Contract density: 0.87
1> CodeContracts: DeleteMe: Total methods analyzed 4
1> CodeContracts: DeleteMe: Methods with 0 warnings 3
1> CodeContracts: DeleteMe: Total time 4.974sec. 1243ms/method
1> CodeContracts: DeleteMe: Methods with necessary preconditions: 1
1> CodeContracts: DeleteMe: Discovered 1 new candidate preconditions in 00:00:00.1718843
1> CodeContracts: DeleteMe: Retained 1 preconditions after filtering
1> CodeContracts: DeleteMe: Inferred 0 object invariants
1> CodeContracts: DeleteMe: Retained 0 object invariants after filtering
1> CodeContracts: DeleteMe: Detected 0 code fixes
1> CodeContracts: DeleteMe: Proof obligations with a code fix: 0
1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(14,10,14,33): warning : CodeContracts: requires is false: x > 0 && y > 0
1>c:\Users\Mike\Documents\Visual Studio 2012\Projects\DeleteMe\DeleteMe\Program.cs(22,10,22,44): warning : CodeContracts: location related to previous warning
1> CodeContracts: Checked 1 assertion: 1 false
1> CodeContracts: DeleteMe:
1> CodeContracts: DeleteMe: Static contract analysis done.
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========