就最佳做法而言,哪个更好:
public void SomeMethod(string str)
{
if(string.IsNullOrEmpty(str))
{
throw new ArgumentException("str cannot be null or empty.");
}
// do other stuff
}
Run Code Online (Sandbox Code Playgroud)
要么
public void SomeMethod(string str)
{
if(str == null)
{
throw new ArgumentNullException("str");
}
if(str == string.Empty)
{
throw new ArgumentException("str cannot be empty.");
}
// do other stuff
}
Run Code Online (Sandbox Code Playgroud)
第二个版本似乎更精确,但也比第一个更麻烦.我通常选择#1,但我想我会检查是否有#2的争论.