Should I Throw ArgumentNullException if a string is blank?

前端 未结 7 1719
独厮守ぢ
独厮守ぢ 2020-12-14 00:11

I working on a method that does something given a string parameter. A valid value for the string parameter is anything other than null or string.Empty. So my code looks li

7条回答
  •  暖寄归人
    2020-12-14 00:21

    Why don't use this code?

    private void SomeMethod(string someArgument)
    {
    //chek only NULL
    if(ReferenceEquals(someArgument,null))
        throw new ArgumentNullException("someArgument");
    
    // and after trim and check
    if (someArgument.Trim() == String.Empty)
        throw new ArgumentException("Input cannot be empty", "someArgument");
    
    // do some work
    }
    

提交回复
热议问题