gpt4 book ai didi

c# - 如何在调用其他构造函数之前抛出 ArgumentNullException

转载 作者:行者123 更新时间:2023-11-30 16:20:08 27 4
gpt4 key购买 nike

我有一个关于构造函数的语法和在构造函数中抛出异常的问题。

如何在调用 CreateAnotherOne() 之前为参数 b 抛出 ArgumentNullException 并像在第二个构造函数中那样抛出异常而不在检查后复制代码?我可以将代码提取到一个单独的私有(private)方法中,但我需要从两个构造函数主体中调用它...是否有其他选项可以实现此目的?

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"));

如果我将检查放入构造函数的主体,在这种情况下它会被检查到很晚。

有什么语法糖可以解决这个问题吗?

最佳答案

私有(private)方法可以做到这一点,但您也可以创建另一个私有(private)构造函数:

    private MyClass(IWhatEver a)
{
if (a == null)
{
throw new ArgumentNullException("a");
}

// the cool code comes here, I could put it into
// the CallMeFromConstructor method but is there another way?
}

public MyClass(IWhatEver a, ISomeThingElse b) : this(a)
{
if (b == null)
{
throw new ArgumentNullException("b");
}
}

public MyClass(IWhatEver a, IAnotherOne b) : this(a)
{
if (b == null)
{
throw new ArgumentNullException("b");
}
}

关于c# - 如何在调用其他构造函数之前抛出 ArgumentNullException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14632446/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com