提示类型参数T与外部类型IDAL类型同名,如何修改

张天星 2018-11-28 10:24:06
public interface IDAL<T>: IDisposable
{
int SaveChanges();
IQueryable<T> All<T>() where T : class; //这里为何报错:提示类型参数T与外部类型IDAL<T>类型同名,如何修改,我希望实现这个接口的时候填入一个类型,然后All或者Find等操作的时候不再需要填写类型
IQueryable<T> Set<T>() where T : class;
T Find<T>(int Id) where T : class;
void Insert<T>(T entity) where T : class;
void Update<T>(T entity) where T : class;
void Delete<T>(T entity) where T : class;
List<T> SqlQuery<T>(string sql);
int ExecuteSqlCommand(string sql);
long GetNextSequenceValue(string sequenceName);
}
...全文
296 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你可以这样改一下[code=csharp]IQueryable<T> All<Q>() where Q : class; /code] 既然你希望在调用 All 的时候不用声明泛型参数,那么就不要这样写 Q,就连 T 也不要写。
  • 打赏
  • 举报
回复
首先,这只是编译器的警告,而不是报错。 你的 All 方法根本没有独立的需要声明的输入参数,而整个类型原本已经有一个用来提供给各个接口定义来共用的泛型参数,因此编译器提示你“重复多遍的话估计是思路有毛病了”。
stherix 2018-11-28
  • 打赏
  • 举报
回复
引用 2 楼 张天星 的回复:
[quote=引用 1 楼 stherix 的回复:] where T :class 要写在接口定义里,下面的方法声明都不需要
没有变,还是这个问题:
public interface IDAL<T> where T : class
    {
        int SaveChanges();
        IQueryable<T> All<T>(); //前面返回值的T没有问题,后面传入的T报错:类型参数T与外部类型IDAL<T>类型同名
        IQueryable<T> Set<T>();
        T Find<T>(int Id);
        void Insert<T>(T entity);
        void Update<T>(T entity);
        void Delete<T>(T entity);
        List<T> SqlQuery<T>(string sql);
        int ExecuteSqlCommand(string sql);
        long GetNextSequenceValue(string sequenceName);
    }
[/quote] All和Set不用加<T> 因为类型已经固定为T了
xuzuning 2018-11-28
  • 打赏
  • 举报
回复
定义本身没有问题,关键在于使用
张天星 2018-11-28
  • 打赏
  • 举报
回复
引用 1 楼 stherix 的回复:
where T :class
要写在接口定义里,下面的方法声明都不需要

没有变,还是这个问题:
public interface IDAL<T> where T : class
{
int SaveChanges();
IQueryable<T> All<T>(); //前面返回值的T没有问题,后面传入的T报错:类型参数T与外部类型IDAL<T>类型同名
IQueryable<T> Set<T>();
T Find<T>(int Id);
void Insert<T>(T entity);
void Update<T>(T entity);
void Delete<T>(T entity);
List<T> SqlQuery<T>(string sql);
int ExecuteSqlCommand(string sql);
long GetNextSequenceValue(string sequenceName);
}
stherix 2018-11-28
  • 打赏
  • 举报
回复
where T :class 要写在接口定义里,下面的方法声明都不需要
张天星 2018-11-28
  • 打赏
  • 举报
回复
引用 4 楼 stherix 的回复:
[quote=引用 2 楼 张天星 的回复:]
[quote=引用 1 楼 stherix 的回复:]
where T :class
要写在接口定义里,下面的方法声明都不需要

没有变,还是这个问题:
public interface IDAL<T> where T : class
{
int SaveChanges();
IQueryable<T> All<T>(); //前面返回值的T没有问题,后面传入的T报错:类型参数T与外部类型IDAL<T>类型同名
IQueryable<T> Set<T>();
T Find<T>(int Id);
void Insert<T>(T entity);
void Update<T>(T entity);
void Delete<T>(T entity);
List<T> SqlQuery<T>(string sql);
int ExecuteSqlCommand(string sql);
long GetNextSequenceValue(string sequenceName);
}
[/quote]

All和Set不用加<T>
因为类型已经固定为T了[/quote]
了解,类型已经在class上预设了,那么方法后面就不需要<T>来指定类型了。

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧