5

I'm experimenting with Code Contract and I encountered one problem. I have class:

public class SpecialPoint
{
    public int X { get; set; }
    public int Y { get; set; }

    public SpecialPoint(int x, int y)
    {
        Contract.Requires<ArgumentException>(y > x);
        X = x;
        Y = y;
    }


    [ContractInvariantMethod]
    private void ClassContract()
    {
        Contract.Invariant(Y > X);
    }
}

and I run a test against it:

[TestFixture]
class SpecialPointTests
{
    [Test]
    public void SpecialPoint()
    {
        var p = new SpecialPoint(10, 20); 
        p.X = 30;
    }
}

I expected static checker to warn me about assignment p.X =30; as this violates invariant but it only takes place during runtime. I have static analysis enable. My version is 1.7.11202.10.

stachu
  • 277
  • 1
  • 3
  • 13

1 Answers1

3

From the MSDN page on Contract.Invariant

During run-time checking, invariants are checked at the end of each public method.

sudheeshix
  • 1,541
  • 2
  • 17
  • 28