I have this simple code :
public interface IReader<out T>
{
IEnumerable<T> GetData();
}
This interface should be covariant on T, and i'm using it this way :
private static Func<bool> MakeSynchroFunc<T>(IReader<T> reader) where T : IComposite
{
return () => Synchronize(reader);
}
Note the constraint for T to implement IComposite.
The synchronization method takes an IReader<IComposite> in input :
private static bool Synchronize(IReader<IComposite> reader)
{
// ......
}
The compiler tells me it cannot convert from IReader<T> to IReader<IComposite> despite the constraint on T and the covariance of IReader.
Is there something i'm doing wrong here ?
The compiler should be able to verify the constraint and the covariance should let me use my IReader<T> as an IReader<Icomposite>, isn't it ?
Thanks.
You should be able to resolve your issue by adding a class constraint to T. Covariance does not work when structs are involved (IEnumerable<int> would not be convertible to IEnumerable<object>). Since you have not constrained T to be a class, you could pass in an IReader<some struct that implements IComposite>, which would not be convertible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With