Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Interfaces can be generic:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading