c# - 与泛型类型方法的接口(interface)

标签 c# .net inheritance interface

我有一个接口(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/

相关文章:

c# - SerialPort.GetPortNames 多次返回同一个端口

c# - 带有锁定文件的 FileStream

c# - MVC4 - 在 DropDownList 中设置初始选定的项目

c# jquery 未将对象引用设置为对象的实例

c# - 如何在 C# 中对事件进行 xml 序列化

c# - 从表中的所有行计算 TotalAmount

Java:为什么需要构造函数来使用父类中的对象?

c++ - 指向抽象模板基类的指针?

c++ - 在父类中为多个 child 定义方法

c# - 从数据库中解析xml