我有以下课程:
class Foo
{
public Foo()
: this(new List<Bar>())
{
}
public Foo(IEnumerable<Bar> bars)
{
Bars = bars;
}
public IEnumerable<Bar> Bars { get; set; }
public Bar GetSingleBar(Data data)
{
// this method returns a single Bar from the Bars property above
// this method returns the Bar which matches the data parameter
// this method should not return null
// this method throws a NoBarsFoundException if
// (a) Bars is empty or
// (b) no bar in Bars matches the data
}
}
Bars
如果是我该怎么办null
?我应该在 setter 中Bars
抛出异常还是应该在 中抛出异常GetSingleBar
?(该方法GetSingleBar
是唯一使用该Bars
属性的方法。)
我应该扔一个ArgumentException
, ArgumentNullException
, InvalidOperationException
, 还是NoBarsFoundException
?