6

Lets say I got a boolean IsValid property on my object.

I would like to create a method, and ensure that IsValid isn't changed after calling it, whether it was true or false before the call.

Is there a support for such thing?

Valentin Kuzub
  • 11,703
  • 7
  • 56
  • 93

4 Answers4

5

For that purpose the [Pure] Attribute has been added to the System.Diagnostic.Contracts Namespace. See here for further explanation. However you cannot prevent a single property from being changed. The method is not allowed to change the object state at all (like the C++ const).

EDIT: Unfortunately the Pure attribute does not work with the current tools. I implemented a test with the following code, no error message either at static nor at runtime type checking:

public class Test
{
    private int x = 0;

    [Pure]
    public void Foo()
    {
        x++;
    }
}

Regarding to the documentation of Pure checks will be supported 'in the future'. Whenever that is ("The Code Contracts team is working heavy on that, thus to come up with a purity checker in a future release.").

I have been using the attribute in the believe it works properly. The documentation says that all methods called within a contract must be declared as pure. It doesn't say whether that's checked or not.

So the answer to your question is: There is no current support for this, but may be in the future.

slfan
  • 8,950
  • 115
  • 65
  • 78
  • 4
    I was about to propose the same thing, but decided to check the msdn. One thing concerns me: "This attribute is not enforced by the current analysis tools; you should use this attribute only if you are sure that the methods are pure." at http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts.pureattribute.aspx. I will do a quick check if it is a problem or not. – ViktorZ Dec 04 '11 at 11:38
  • Confirmed - PureAttribute doesn't produce warning or error in code contract analyzer in applied to a method that changes object state. Tested both with runtime and static checking. So there is no way to use Code Contracts for this scenario – ViktorZ Dec 04 '11 at 11:53
  • That's an interesting point, I confirmed it too! So microsoft implements and documents an attribute which is of no actual use. That's bad – slfan Dec 04 '11 at 11:55
  • hm there has to be SOME purpose for it? Or its just announced feature thats just not implemented? Sounds very odd. – Valentin Kuzub Dec 04 '11 at 11:58
  • I just checked the documentation. They seem to be used only in combination with code contracts and declare that there are no visible side effects to the method. It sais that 'in the future' there will be a purity checker. Very sad... – slfan Dec 04 '11 at 12:10
  • NB: declaring Purity only applies to PUBLIC state (ie: visible to caller), not private/internal state. – koenmetsu Dec 05 '11 at 08:35
3

I haven't tried it myself, but according to the MSDN Contract.OldValue might help to check that a single property value has not changed:

public bool IsValid
{
  get
  {
    ...
  }
}

public void SomeMethod()
{
  Contract.Ensures(this.IsValid == Contract.OldValue(this.IsValid));
  ...
}
Pavel Gatilov
  • 7,579
  • 1
  • 27
  • 42
1

No, unfortunately c# doesn't provide a const logic such c++ does.

AlexTheo
  • 4,004
  • 1
  • 21
  • 35
1

The only way to go about doing this is to control your code in such a way that you know it won't change. There is no specific code or syntax to control this otherwise (as in C++).

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55