1

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

Amit Raz
  • 5,370
  • 8
  • 36
  • 63

2 Answers2

2

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...

Amit Raz
  • 5,370
  • 8
  • 36
  • 63
1

If you want to have the squiggly lines, then you have to check 'Show squigglies':

enter image description here

The re-build the project, wait for the static analysis to finish and you will have both warnings in 'Output' window (if you're not running the 'Ultimate' version of VS, these can be easy to miss) and the lines under suspicious code.

[Edit] By the way, I always run with the 'Standard Contract Requires' Assembly Mode.

Then after the example build: enter image description here

and in the IDE:

enter image description here

Patryk Ćwiek
  • 14,078
  • 3
  • 55
  • 76