T must be contravariantly valid

前端 未结 3 1505
灰色年华
灰色年华 2020-12-01 11:44

What is wrong with this?

interface IRepository where T : IBusinessEntity
{
    IQueryable GetAll();
    void Save(T t);
    void Delete         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-01 12:38

    The following two methods are wrong:

    void Save(T t);
    void Delete(T t);
    

    You can't have T as method argument. Only as return type if you want it to be covariant (out T) in your generic definition.

    Or if you want contravariance then you could use the generic parameter only as method argument and not return type:

    interface IRepository where T : IBusinessEntity
    {
        void Save(T t);
        void Delete(T t);
    }
    

提交回复
热议问题