How to set an exception property for purity?

106 views Asked by At

I have class like bellow and I want to only let x be changed in Foo method and no other property. I can't use [Pure] like following example, because it locks all properties:

public class Test
{
    private int x,y,z; //number of these properties is large

    [Pure]
    public void Foo()
    {
        //only x must be allowed to change
    }
}

and I don't want to use something like this for all other properties than x:

Contract.Ensures(Contract.OldValue<int>(y) == y);
Contract.Ensures(Contract.OldValue<int>(z) == z);
...//and for other large number of properties

Is there any way to do this?

2

There are 2 answers

0
Evgeniy Mironov On

Unfortunately, standard way with Contracts I'm not found.

But you may use this way (this way have some constraints):

    public class Test
    {
        public int x, y, z;//....

        public void Foo()
        {
            x = FooBody();
        }

        [Pure]
        private int FooBody()
        {
            int value = x;
            //work with value as x
            return value;
        }
    }
0
Majid On

It seems there is no method implemented for this purpose in Contract class.