在 c# 中,我可以这样做:
public int Foo { get; set; }
这很好。但是,一旦我想在 getter 或 setter 中做任何事情,我就必须将其更改为:
private int foo;
public int Foo {
get { return foo; }
set {
foo = value;
DoSomething(); // all that other code, just to add this line!
}
}
有可能避免这种情况吗?我希望能够做这样的事情:
public int Foo {
get;
set {
DoSomething();
}
}
我能离上面多近?