Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 427
stakx - no longer contributing Avatar asked Jan 28 '10 11:01

stakx - no longer contributing


People also ask

Which of the following methods is used in pre conditions and post conditions for code contracts in net?

NET 4.0. Code Contracts API includes classes for static and runtime checks of code and allows you to define preconditions, postconditions, and invariants within a method.

Which method of the contract class is used to implement precondition contracts?

Code contracts provide a way to specify preconditions, postconditions, and object invariants in . NET Framework code. Preconditions are requirements that must be met when entering a method or property.

What are the code contracts?

Code Contracts provide a language-agnostic way to express coding assumptions in . NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs.

What is contract class in C#?

Code contract classes let you specify preconditions, postconditions, and object invariants in your code. Preconditions are requirements that must be met when entering a method or property. Postconditions describe expectations at the time the method or property code exits.


1 Answers

Contract.OldValue should be enough:

Contract.Ensures(this.SomeProperty == Contract.OldValue(this.SomePropety));
like image 196
Pent Ploompuu Avatar answered Sep 29 '22 03:09

Pent Ploompuu