代码合同警告未显示

时间:2012-05-22 11:50:11

标签: c# .net code-contracts

我有这个简单的代码:

public ArrayStack(int capacity)
    {
        Contract.Requires(capacity >= 0);
        Contract.Ensures(_items != null);
        Contract.Ensures(_items.Length == capacity);
        _items = new T[capacity];
        _top = -1;
    }

我预计一旦我输入followig,我将收到编译时警告,但我只从合同中获得运行时异常。

static void Main(string[] args)
    {
        int i = -1;
        ArrayStack<string> stack = new ArrayStack<string>(i);

    }

任何想法?

已编辑:我的代码合同设置的图片 enter image description here

2 个答案:

答案 0 :(得分:2)

想出来。

似乎编译器太聪明了,看到没有人在最后一行之后使用堆栈,所以他没有检查它。

一旦我添加了stack.push(...),它就会给我错误...

让计算机太聪明了......

答案 1 :(得分:1)

如果你想拥有波浪线,那么你必须检查'Show squigglies':

enter image description here

重新构建项目,等待静态分析完成,然后在“输出”窗口中有两个警告(如果你没有运行VS的'Ultimate'版本,这些很容易错过)以及可疑代码下的行。

[编辑]顺便说一下,我总是使用'​​标准合同要求'装配模式。

然后在示例构建之后: enter image description here

并在IDE中:

enter image description here

相关问题