This is covariance problem. IEnumerable<T>
is covariant, ICollection<T>
is not.
public interface IEnumerable<out T> : IEnumerable
public interface ICollection<T> : IEnumerable<T>, IEnumerable
You can see the out
keyword only in IEnumerable<T>
declaration.
That's why your extension method works only for ICollection<IInterface>
, and not for ICollection<ClassWhichImplementsIInterface>
.
Make your extension methods generic to make it work:
internal static class MyExtensions
{
internal static ICollection<T> Foo<T>(this ICollection<T> c1, IEnumerable<T> c2) where T : IInterface
{
//implementation removed for posting
return c1;
}
internal static ICollection<T> Foo<T>(this ICollection<T> c1, T thing) where T : IInterface
{
return Foo(cache, new List<T>() { thing });
}
}
or
internal static class MyExtensions
{
internal static ICollection<IInterface> Foo<T>(this ICollection<IInterface> c1, IEnumerable<T> c2) where T : IInterface
{
//implementation removed for posting
return c1;
}
internal static ICollection<IInterface> Foo<T>(this ICollection<T> c1, T thing) where T : IInterface
{
return Foo(cache, new List<IInterface>() { thing });
}
}
Depending what you want it to return: ICollection<IInterface>
or ICollection<T>
.