开发者

Code Contracts: How do I state in a post-condition that a field/property's value has not changed?

开发者 https://www.devze.com 2022-12-18 21:45 出处:网络
I\'ll best just show with a code example what I would like to accomplish? class SomeClass { public int SomeProperty;

I'll best just show with a code example what I would like to accomplish?

class SomeClass
{
    public int SomeProperty;

    public void SomeOperation()
    {
        Contract.Ensures( "SomeProperty's value has not changed." );
                     //   ^^^^^^^^^^^^^^^^^^^^开发者_运维知识库^^^^^^^^^^^^^^^^^^^
                     //    How can I write this post-condition?
    }
};

(The string passed to Contract.Ensures() is of course just a placeholder for the real post-condition expression.)

How can I do this? Would Contract.OldValue<>() be of any use here?


Contract.OldValue should be enough:

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

精彩评论

暂无评论...
验证码 换一张
取 消