Just want to make simple extension for syntactic sygar :
public static bool IsNotEmpty(this ICollection obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
public static bool IsNotEmpty<T>(this ICollection<T> obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
It works perfectly when I work with some collections, but when working with others I get
The call is ambiguous between the
following methods or properties:
‘PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.IList)’
and
‘PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.Generic.ICollection)’
Is there any canonical solution to this problem ?
No, I don’t want to perform a cast before calling this method 😉
My best way to solve the ambiguity : define an overload for all common non-generic ICollection classes.
That means custom ICollection won’t be compatible, but it’s no big deal as generics are becoming the norme.
Here is the whole code :
Note that I did not want it to work on
IEnumerable<T>, becauseCount()is a method that can trigger a database request if you are working with Linq-to-Entity or Linq-to-SQL.