Throwing ArgumentNullException

前端 未结 12 2211
名媛妹妹
名媛妹妹 2020-12-08 02:10

Suppose I have a method that takes an object of some kind as an argument. Now say that if this method is passed a null argument, it\'s a fatal error and an exception shoul

12条回答
  •  隐瞒了意图╮
    2020-12-08 02:22

    I'd prefer the parameter check with the explicit ArgumentNullException, too.

    Looking at the metadata:

     //
        // Summary:
        //     Initializes a new instance of the System.ArgumentNullException class with
        //     the name of the parameter that causes this exception.
        //
        // Parameters:
        //   paramName:
        //     The name of the parameter that caused the exception.
        public ArgumentNullException(string paramName);
    

    You can see, that the string should be the name of the parameter, that is null, and so give the developer a hint on what is going wrong.

提交回复
热议问题