List <string>在C#</string>中检查null

时间:2014-03-08 06:00:16

标签: c# c#-4.0

我在C#中有一个函数。代码如下:

public IList<VW_CANDBASICSEARCH> GetAdvSearchCandidate(List<string> strSkill, List<string> strRole, string strOrganization, string strPosition, string strLocation)
{

    //Code goes here
}

当我调用函数时有时strSkill或strRole变为null。但是如何检查strRole / strSkill?我试过strSkill!= null但是它给出了错误。

1 个答案:

答案 0 :(得分:1)

public IList<VW_CANDBASICSEARCH> GetAdvSearchCandidate(List<string> strSkill, List<string> strRole, string strOrganization, string strPosition, string strLocation)
{
    if(strSkill == null)
    {
        throw new ArgumentNullException("strSkill");
    }

    // do all other checks

    // your code
}

空检查几乎是标准程序。

SideNote:您可能希望阅读一本关于C#编码标准的好书。除了空检查之外,它还解释了变量和函数命名。您的代码看起来像是如何不这样做的示例。

作为书籍:Framework Design Guidelines或MSDN页面here