Check whether a read-only collection is null or empty - CSharp System.Collections.ObjectModel

CSharp examples for System.Collections.ObjectModel:ReadOnlyCollection

Description

Check whether a read-only collection is null or empty

Demo Code

//    Copyright (c) CloudLibrary 2016.  All rights reserved.
using System.Collections.Generic;
using System.Collections;

public class Main{
        /// <summary>
        /// Check whether a read-only collection is null or empty
        /// </summary>
        /// <typeparam name="T">type of item in collection</typeparam>
        /// <param name="collection">collection to check</param>
        /// <returns>true if collection is either null or empty. False if collection is not null and contains item</returns>
        /// <remarks>
        /// String has a method called IsNullOrEmpty. We need similar method for collection because it is used wildly.
        /// </remarks>
        /// <example>
        /// Following is example of how to use it:
        /// <![CDATA[
        /// void Foo(IReadOnlyCollection<int> dataToProcess)
        /// {//from   ww  w .  j av  a 2  s. co  m
        ///     if (dataToProcess.IsNullOrEmpty())
        ///     {
        ///         throw new ArgumentException("data should not be null or empty");
        ///     }
        ///     ...
        /// }
        /// ]]>
        /// </example>
        public static bool IsReadOnlyNullOrEmpty<T>(this IReadOnlyCollection<T> collection)
        {
            return (collection == null) || (collection.Count < 1);
        }
}

Related Tutorials