代码之家  ›  专栏  ›  技术社区  ›  stakx - no longer contributing Saravana Kumar

代码契约:如何在post条件下声明字段/属性的值没有更改?

  •  7
  • stakx - no longer contributing Saravana Kumar  · 技术社区  · 15 年前

    我最好用一个代码示例来说明我想要完成什么?

    class SomeClass
    {
        public int SomeProperty;
    
        public void SomeOperation()
        {
            Contract.Ensures( "SomeProperty's value has not changed." );
                         //   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                         //    How can I write this post-condition?
        }
    };
    

    (传递给 Contract.Ensures() 当然只是实际post条件表达式的占位符。)

    我该怎么做?会 Contract.OldValue<>() 这里有什么用吗?

    1 回复  |  直到 15 年前
        1
  •  5
  •   Pent Ploompuu    15 年前

    Contract.OldValue 应该足够了:

    Contract.Ensures(this.SomeProperty == Contract.OldValue(this.SomePropety));