想象一个默认属性:
class Positive {
public int Value { get; set; }
}
我想添加一个前提条件:该set
值只能是正数。是否可以在不添加成员变量样板的情况下做到这一点?
public int Value { get;
set {
if(value < 0) throw new ArgumentOutOfBoundsException();
// continue doing 'the default thing'
// instead of `value_=value`, mirrored by a change in the
// get, and adding the `int value_` member variable
}
};