Check if a collection is empty or just a null. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:ICollection

Description

Check if a collection is empty or just a null.

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
using System;//from   w  ww  . jav a  2  s  .c  om

public class Main{
        /// <summary>
        /// Check if a collection is empty or just a null.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection">the ref of collection</param>
        /// <returns>return true if the collection is null or empty, 
        /// otherwise return false</returns>
        public static bool IsNullOrEmpty<T>(this IEnumerable<T> collection)
        {
            return collection == null || !collection.Any();
        }
}

Related Tutorials