I know generally empty List is more prefer than NULL. But I am going to return NULL, for mainly two reasons
Late update: since C# 6.0, the null-propagation operator may be used to express concise like this:
if ( list?.Count > 0 ) // For List
if ( array?.Length > 0 ) // For Array
or, as a cleaner and more generic alternative for IEnumerable:
if ( enumerable?.Any() ?? false )
Note 1: all upper variants reflect actually IsNotNullOrEmpty, in contrast to OP question (quote):
Because of operator precedence
IsNullOrEmptyequivalents look less appealing:
if (!(list?.Count > 0))
Note 2: ?? false is necessary, because of the following reason (summary/quote from this post):
?.operator will returnnullif a child member isnull. But [...] if we try to get a non-Nullablemember, like theAny()method, that returnsbool[...] the compiler will "wrap" a return value inNullable<>. For example,Object?.Any()will give usbool?(which isNullable), notbool. [...] Since it can't be implicitly casted toboolthis expression cannot be used in theif
Note 3: as a bonus, the statement is also "thread-safe" (quote from answer of this question):
In a multithreaded context, if [enumerable] is accessible from another thread (either because it's a field that's accessible or because it's closed over in a lambda that is exposed to another thread) then the value could be different each time it's computed [i.e.prior null-check]