相关疑难解决方法(0)

ArgumentException与ArgumentNullException?

我正在重构一些代码并添加一个方法来替换(即将推出)已弃用的方法.新方法具有以下特征:

FooResult Foo(FooArgs args) { ... }
Run Code Online (Sandbox Code Playgroud)

不推荐使用的方法包含越来越多的参数.这些参数现在是FooArgs类的属性.不推荐使用的方法有几个保护条件,它使用以下结构检查空值:

if (parameter1 == null)
    throw new ArgumentNullException(“parameter1”);
if (parameter... == null)
    throw new ArgumentNullException(“parameter...”);
if (parameterN == null)
    throw new ArgumentNullException(“parameterN”);
Run Code Online (Sandbox Code Playgroud)

既然已将参数折叠到FooArgs类中,我应该为参数的各个属性抛出ArgumentNullExceptionFooArgs:

if (args.Property1 == null)
    throw new ArgumentNullException(“args.Property1”);
if (args.Property... == null)
    throw new ArgumentNullException(“args.Property...”);
if (args.PropertyN == null)
    throw new ArgumentNullException(“args.PropertyN”);
Run Code Online (Sandbox Code Playgroud)

或者为整个参数抛出更一般的ArgumentException: FooArgs

if (args.Property1 == null)
    throw new ArgumentException(“Property1 cannot be null.”, “args”);
if (args.Property... == …
Run Code Online (Sandbox Code Playgroud)

.net c#

21
推荐指数
2
解决办法
5790
查看次数

标签 统计

.net ×1

c# ×1