Why doesn't Any() work on a c# null object

前端 未结 8 1285
孤城傲影
孤城傲影 2021-02-02 05:33

When calling Any() on a null object, it throws an ArgumentNullException in C#. If the object is null, there definitely aren\'t \'any\', and it should probably return false.

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-02 06:06

    Because Any() it is a extension method like this:

    public static bool Any(this IEnumerable enumerable)
    {
        if (enumerable == null)
            throw ArgumentNullException("enumerable");
        ...
    }
    

提交回复
热议问题