相关疑难解决方法(0)

字符串参数的ArgumentException或ArgumentNullException?

就最佳做法而言,哪个更好:

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的争论.

c# exception

33
推荐指数
2
解决办法
9971
查看次数

标签 统计

c# ×1

exception ×1