Search code examples
c#.netcode-contracts

Code Contracts warning not showing


I have this simple code:

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

I expected that once I type the followig I will get a compile time warning, but I only get a runtime exception from the contract.

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

    }

any ideas?

EDITED: picture of my code contract settings enter image description here


Solution

  • Figured it out.

    It seems the compiler is too smart and sees no one is using the stack after the last line so he does not check it.

    once I add stack.push(...) it gives me the error...

    cant have the coputer to be too smart...