Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C# have IsNullOrEmpty for List/IEnumerable?

I know generally empty List is more prefer than NULL. But I am going to return NULL, for mainly two reasons

  1. I have to check and handle null values explicitly, avoiding bugs and attacks.
  2. 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?

like image 505
Eric Yin Avatar asked Dec 20 '11 21:12

Eric Yin


People also ask

What is the main cause of hep C?

Hepatitis C is a liver infection caused by the hepatitis C virus (HCV). Hepatitis C is spread through contact with blood from an infected person. Today, most people become infected with the hepatitis C virus by sharing needles or other equipment used to prepare and inject drugs.

Does hep C go away?

Hepatitis C virus (HCV) causes both acute and chronic infection. Acute HCV infections are usually asymptomatic and most do not lead to a life-threatening disease. Around 30% (15–45%) of infected persons spontaneously clear the virus within 6 months of infection without any treatment.

What does hep C pain feel like?

Many people with chronic HCV suffer from aches and pains in their joints. A variety of different joints can be involved but the most common are in the hands and wrists. These pains are often minor but occasionally the pain can be quite severe. In such cases painkillers can be used to relieve the symptoms.

Does hep C treatment make you sick?

Like the other antivirals, the side effects are mild. You might have a slight headache or bellyache, or you might feel tired. Glecaprevir and pibrentasvir (Mavyret): Three pills daily can treat all types of hep C. Side effects are mild and can include headache, fatigue, diarrhea, and nausea.


2 Answers

nothing baked into the framework, but it's a pretty straight forward extension method.

See here

/// <summary>     /// Determines whether the collection is null or contains no elements.     /// </summary>     /// <typeparam name="T">The IEnumerable type.</typeparam>     /// <param name="enumerable">The enumerable, which may be null or empty.</param>     /// <returns>     ///     <c>true</c> if the IEnumerable is null or empty; otherwise, <c>false</c>.     /// </returns>     public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)     {         if (enumerable == null)         {             return true;         }         /* If this is a list, use the Count property for efficiency.           * The Count property is O(1) while IEnumerable.Count() is O(N). */         var collection = enumerable as ICollection<T>;         if (collection != null)         {             return collection.Count < 1;         }         return !enumerable.Any();      } 

Daniel Vaughan takes the extra step of casting to ICollection (where possible) for performance reasons. Something I would not have thought to do.

like image 75
Matthew Vines Avatar answered Sep 23 '22 17:09

Matthew Vines


Late update: since C# 6.0, the null-propagation operator may be used to express concise like this:

if (  list?.Count  > 0 ) // For List<T> if ( array?.Length > 0 ) // For Array<T> 

or, as a cleaner and more generic alternative for IEnumerable<T>:

if ( enumerable?.Any() ?? false ) 

Note 1: all upper variants reflect actually IsNotNullOrEmpty, in contrast to OP question (quote):

Because of operator precedence IsNullOrEmpty equivalents look less appealing:
if (!(list?.Count > 0))

Note 2: ?? false is necessary, because of the following reason (summary/quote from this post):

?. operator will return null if a child member is null. But [...] if we try to get a non-Nullable member, like the Any() method, that returns bool [...] the compiler will "wrap" a return value in Nullable<>. For example, Object?.Any() will give us bool? (which is Nullable<bool>), not bool. [...] Since it can't be implicitly casted to bool this expression cannot be used in the if

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]

like image 33
Teodor Tite Avatar answered Sep 19 '22 17:09

Teodor Tite