If you use only the IProvider<IElement>
link to access methods that have T
in the output position, you can split the interface into two (find more suitable names for them, for example, ISink<in T>
for contravariant)
public interface IProviderOut<out T> where T : IElement { IEnumerable<T> Provide(); } public interface IProviderIn<in T> where T : IElement { void Add(T t); }
Both are implemented in your class:
public class MyProvider : IProviderOut<MyElement>, IProviderIn<MyElement> { public IEnumerable<MyElement> Provide() { ... } public void Add(MyElement t) { ... } }
But now you are using a covariant interface when you need to speed up:
IProviderOut<IElement> provider = new MyProvider();
In addition, your interface can inherit from both:
public interface IProvider<T> : IProviderIn<T>, IProviderOut<T> where T : IElement {
And your class implements it:
public class MyProvider : IProvider<MyElement> ...
source share