简体   繁体   中英

How to validate the List parameter in c# to fix CA1062

I have following code. I getting the FxCop violation. I do not know how to validate the List parameter.

 public Collection<ProjectData> IsHookedConfiguredList(Collection<ProjectData> groupProjectList)
    {
        if (groupProjectList.Count != 0)
        {
           // my code
        }            

        return hookConfiguredList;
    }

I added the line if (groupProjectList.Count != 0) But I did not rid violation.

How could I fix this?

Code Analysis wants you to check whether passed argument is null before you use it. Try this:

public Collection<ProjectData> IsHookedConfiguredList(Collection<ProjectData> groupProjectList)
{
    if (groupProjectList == null)
    {
        throw new ArgumentNullException(nameof(groupProjectList));
    }

    //  the code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM