xto*_*ofl 1 c# properties preconditions
想象一个默认属性:
class Positive {
public int Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想补充一个先决条件: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
}
};
Run Code Online (Sandbox Code Playgroud)