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 helper class? The reason I am asking is that in if
statements the code looks cluttered if the patter is (mylist != null && mylist.Any())
. It would be much cleaner to have Foo.IsAny(myList)
.
This post doesn't give that answer: IEnumerable is empty?.
I use
Bool IsCollectionNullOrEmpty = !(Collection?.Any()??false);
. Hope this helps.Breakdown:
Collection?.Any()
will returnnull
if Collection is null, andfalse
if Collection is empty.Collection?.Any()??false
will give usfalse
if Collection is empty, andfalse
if Collection isnull
.Complement of that will give us
IsEmptyOrNull
.