ArgumentException vs. ArgumentNullException?

前端 未结 4 1065
予麋鹿
予麋鹿 2021-01-03 20:13

I’m refactoring some code and adding a method which will replace a (soon-to-be) deprecated method. The new method has the following signature:

FooResult Foo         


        
4条回答
  •  北海茫月
    2021-01-03 21:09

    You need to add a check for the args itself to be non-null. The ANE is not appropriate for individual components, so you need to use more general AE, like this:

    if (args == null)
        throw new ArgumentNullException(“args”);
    if (args.Property1 == null)
        throw new ArgumentException(“Property1 cannot be null.”, “args”);
    if (args.Property... == null)
        throw new ArgumentException(“Property... cannot be null.”, “args”);
    if (args.PropertyN == null)
        throw new ArgumentException(“Property2 cannot be null.”, “args”);
    

提交回复
热议问题