IsNullOrEmpty generic method for Array to avoid Re-Sharper warning

Posted by Michael Freidgeim on Geeks with Blogs See other posts from Geeks with Blogs or by Michael Freidgeim
Published on Tue, 29 May 2012 11:52:30 GMT Indexed on 2012/05/30 16:43 UTC
Read the original article Hit count: 261

Filed under:
I’ve used the following extension method in many places.

public static bool IsNullOrEmpty(this Object[] myArr)
{
return (myArr == null || myArr.Length == 0);
}

Recently I’ve noticed that Resharper shows warning covariant array conversion to object[] may cause an exception for the following code

ObjectsOfMyClass.IsNullOrEmpty()


I’ve resolved the issue by creating generic extension method

public static bool IsNullOrEmpty<T>(this T[] myArr)
{
return (myArr == null || myArr.Length == 0);
}

Related links

http://connect.microsoft.com/VisualStudio/feedback/details/94089/add-isnullorempty-to-array-class
   public static bool IsNullOrEmpty(this System.Collections.IEnumerable source)
       {
           if (source == null)
               return true;
           else
           {
               return !source.GetEnumerator().MoveNext();
           }
       }

http://stackoverflow.com/questions/8560106/isnullorempty-equivalent-for-array-c-sharp

© Geeks with Blogs or respective owner