Search code examples
c#parametersinterfaceabstract-class

How can I implement an interface in the below code?


I want to implement an interface which can be used in both Projct and Employe classes. How should I create Add() and ViewAll() methods in the interface, so that I don't have to overload the methods in classes while declaring as the method name is same, but the parameters are different.

public class Employe
    {
        public List<Employee> Employees { get; set; } = new List<Employee>();

        public void Add(Employee employee)
        {
            Employees.Add(employee);
        }

        public List<Employee> ViewAll()
        {
            return Employees;
        }

    }
    public class Projct
    {      

        public List<Project> Projects { get; set; } = new List<Project>();

        public void Add(Project project)
        {
            Projects.Add(project);
        }

        public List<Project> ViewAll()
        {
            return Projects;
        }
}

I understand that Interface is like a contract and it doesn't make sense to change the parameters. But the question is regarding implementation. Also, I have seen majority of threads related to this topic, saw answers related to declaring parameter class or using params and even tried doing that. I still can't figure it out, so if someone can explain from a simple perspective, that would be welcome.


Solution

  • Interfaces can be generic:

    public interface IMyInterface<T> {
        void Add(T t);
        List<T> ViewAll();
    }