我有一个接口(interface)
interface Repository {
...
List<T> GetAll<T>();
...
}
我应该如何实现该方法?编译器说我不能像下面那样做,因为我返回的不是通用列表
public class EmployeeRepository : Repository {
...
public List<T> GetAll<T>() {
List<Employee> employees = new List<Employee>();
...
return employees;
}
最佳答案
使 Repository
成为通用接口(interface):
interface Repository<T> {
List<T> GetAll();
}
public class EmployeeRepository : Repository<Employee> {
public List<Employee> GetAll() {
List<Employee> employees = new List<Employee>();
return employees;
}
}
关于c# - 与泛型类型方法的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521564/