【发布时间】:2009-03-20 03:33:13
【问题描述】:
在 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();
}
}
我离上面还有多远?
【问题讨论】:
标签: c# properties automatic-properties