I know generally empty List is more prefer than NULL. But I am going to return NULL, for mainly two reasons
- I have to check and handle null values explicitly, avoiding bugs and attacks.
- It is easy to perform
??
operation afterwards to get a return value.
For strings, we have IsNullOrEmpty. Is there anything from C# itself doing the same thing for List or IEnumerable?
nothing baked into the framework, but it's a pretty straight forward extension method.
See here
Daniel Vaughan takes the extra step of casting to ICollection (where possible) for performance reasons. Something I would not have thought to do.
Late update: since C# 6.0, the null-propagation operator may be used to express concise like this:
Note 1:
?? false
is necessary, because of the following reason (summary/quote from this post):Note 2: as a bonus, the statement is also "thread-safe" (quote from answer of this question):
Putting together the previous answers into a simple extension method for C# 6.0+:
As everyone else has said, nothing is built into the framework, but if you are using Castle then Castle.Core.Internal has it.
There is nothing built in.
It is a simple extension method though: