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

前端 未结 8 1286
孤城傲影
孤城傲影 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:08

    As others have already mentioned, Any checks whether or not a sequence contains elements. It does not prevent you from passing null values(what might the bug in the first place).

    Every extension method in Enumerable class throws an an ArgumentNullException if the source is null. Throwing ArgumentNullExceptions in extensions actually is good practise.

提交回复
热议问题