如何确定变量是动态增加还是减少/检测变化

Kev*_*sen 2 c# math variables memory-address

所以,我试图找出,如何确定我的变量是否正在增加它的价值,或者减少它.我知道我可以使用一个变量来存储旧值,但在这种情况下它不是一个选项.

int variable = 0;
variable = 1;
if(variable has increased) {
      //Do Magic stuff
}
Run Code Online (Sandbox Code Playgroud)

基本上,我会怎么想这样做.我不知道是否有可能这样,没有旧值的容器,但我认为可能有一个C#函数,可以解决这个问题,可能是一个内存地址?

我还没有得到任何线索,这种方法或技术也被称之为,所以对此也很了解.

Mit*_*eat 5

使变量成为属性(在类中).

在该属性的setter中,记录变量每次设置时是增加还是减少.

例如:

class Class1
{
    private int _counter;
    private int _counterDirection;

    public int Counter
    {
        get { return _counter; }

        set
        {
            if (value > _counter)
            {
                _counterDirection = 1;
            }
            else if (value < _counter)
            {
                _counterDirection = -1;
            }
            else
            {
                _counterDirection = 0;
            }
            _counter = value;
        }
    }

    public int CounterDirection()
    {
         return _counterDirection;
    }
}
Run Code Online (Sandbox Code Playgroud)