How to check if IEnumerable is null or empty?

前端 未结 22 1049
梦如初夏
梦如初夏 2020-11-27 12:33

I love string.IsNullOrEmpty method. I\'d love to have something that would allow the same functionality for IEnumerable. Is there such? Maybe some collection he

22条回答
  •  再見小時候
    2020-11-27 13:06

    I use Bool IsCollectionNullOrEmpty = !(Collection?.Any()??false);. Hope this helps.

    Breakdown:

    Collection?.Any() will return null if Collection is null, and false if Collection is empty.

    Collection?.Any()??false will give us false if Collection is empty, and false if Collection is null.

    Complement of that will give us IsEmptyOrNull.

提交回复
热议问题