我有一个与构造函数的语法和在构造函数中抛出异常有关的问题。
如何在调用 CreateAnotherOne() 之前为参数 b 抛出 ArgumentNullException 并在第二个构造函数中抛出异常,而不在检查后复制代码?我可以将代码提取到一个单独的私有方法中,但我需要从两个构造函数主体中调用它......还有其他选择来实现这一点吗?
public class MyClass
{
public MyClass(IWhatEver a, ISomeThingElse b)
: this(a, b != null ? b.CreateAnotherOne() : null)
{
// or should I call :this(a, b.CreateAnotherOne()) instead? this could cause a NullReferenceException => how to verify that b is not null?
// don't want to call CallMeFromConstructor() instead of call to other constructor
// would not do the following (why should I check a/c twice?) and the check is too late here (because already called a method on b, b.CreateAnotherOne())
if (a == null)
{
throw new ArgumentNullException("a");
}
if (b == null)
{
throw new ArgumentNullException("b");
}
}
public MyClass(IWhatEver c, IAnotherOne d)
{
if (c == null)
{
throw new ArgumentNullException("c");
}
if (d == null)
{
throw new ArgumentNullException("d");
}
// the cool code comes here, I could put it into
// the CallMeFromConstructor method but is there another way?
}
...
private void CallMeFromConstructors()
{
// the cool code could be here, too (but is there another way?)
}
如果我用 : this(a, b != null ? b.CreateAnotherOne() : null) 调用第二个构造函数,我会得到一个 ArgumentNullException for d int 第二个构造函数。这对我来说听起来很奇怪并且可能会产生误导,因为我调用了第一个(只能在堆栈跟踪中看到)。
问题是我不会写
:this(a, b == null ? b.CreateAnotherOne() : throw new ArgumentNullException("b"));
如果我将检查放入构造函数的主体中,在这种情况下它会被检查到很晚。
任何语法糖的想法来解决这个问题?