IsNullOrEmpty generic method for Array to avoid Re-Sharper warning

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
posted @ Tuesday, May 29, 2012 10:52 PM
Print

Comments on this entry:

No comments posted yet.

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«May»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678