检测变量变化C# [英] Detect variable change c#
问题描述
我已经搜索了一段时间,但没有发现任何问题.
I've been searching this for a while and I didn't found anything for my problem.
我有一个整数:
private static int kills = 0;
我希望在该变量更改时运行一个函数.就像现在是0.如果将其更改为2,则希望调用类似 OnVarChange 的函数,而该函数 OnVarChange 将返回已更改的金额.在这种情况下2-0 = 2,因此它将返回2.
I want a function to run when that variable changes. Like, it is 0 now. If it changes to 2, I want a function like OnVarChange that will be called, and that function OnVarChange will return the amount that was changed. In this case 2-0=2, so it will return 2.
这可能吗?我该怎么办?
Is this possible? How do I do it?
希望您能理解我刚才说的:p
Hope you understand what I just said :p
推荐答案
您需要提供一种更改机制:
You need to provide a change mechanism:
添加:
public static int Kills{
get{ return kills; }
set{
kills = value;
//change code here...
}
}
仅使用此公共 Kills
属性进行 set
设置.如果可能,请勿直接更改实例成员 kills
.但是总有例外.
Only set
using this public Kills
property. Don't directly change the instance member kills
if possible. But there is always an exception.
这篇关于检测变量变化C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!