0
public interface IMasterService<out T> : IDisposable
{
        T GetByID(int id);
        IEnumerable<T> GetAll();
        IEnumerable<T> Where(Expression<Func<T, bool>> func);
        int AddNew(T newEntity);
}

Invalid variance: The type parameter 'T' must be contravariantly valid on 'IMasterService.Where(System.Linq.Expressions.Expression>)'. 'T' is covariant.

rocky
  • 157
  • 3
  • 13
  • remove the out in `` will avoid error, but I think (hope) you didn't put it just for fun ? – Raphaël Althaus Jul 27 '12 at 09:27
  • I surely need to implement my covariance with Generics. It works well with GetByID and GetAll Methods. however, where I try work with Where(Expression> it gives me that error. – rocky Jul 27 '12 at 09:36

1 Answers1

0

In short: if you want to use <out T> then you should return T and if you want to use <in T> you need to pass T as a parameter. It's not possible to use both out and in on one interface. Right now I can't say why exactly you can't use Expression<Func<T, bool>> but I'll try to find out something on it.

Alexander
  • 1,299
  • 9
  • 13
  • Please find below the complete Code and see what I am trying to achieve. Basically Covarince with Generics. It works well for 1st two methods (GetByID & GetAll). But it fails with Where. It gives an error Invalid variance: The type parameter 'T' must be contravariantly valid on 'IMasterService.Where(System.Linq.Expressions.Expression>)'. 'T' is covariant. Find code here http://www.scribd.com/doc/101216821/Code – rocky Jul 27 '12 at 12:18
  • `Right now I can't say why exactly you can't use Expression> but I'll try to find out something on it.`, plz checkout https://stackoverflow.com/questions/11452614/variance-in-expressionfunct-bool – n0099 May 22 '23 at 18:25
  • `It's not possible to use both out and in on one interface.`, actually you can, e.g. `interface Func` – n0099 May 22 '23 at 18:27