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

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

    The Any method runs against an IEnumerable and tells you whether there are any items in the Enumerable. If you don't give it anything to enumerate then an ArgumentNullException is reasonable: a collection with no (matching) elements is different to no collecion.

提交回复
热议问题