mardi 4 juillet 2017

Interface reference that specifies I need an object implementing 2 or more interfaces

Let us say I have this overly simple code here:

class Person
{
    public int age;
    public string name;
    public Person(int age, string name)
    {
        this.age = age;
        this.name = name; 
    }
}
public class MySimpleDatabase
{
    List<Person> people;

    internal List<Person> People
    {
        get
        {
            if(people == null)
            {
                people = new List<Person>();
                people.Add(new Person(24, "ASD"));
                people.Add(new Person(35, "QWE"));
                people.Add(new Person(12, "MNB"));
            }
            return people;
        }
    }
}
public class ProcessPeopleConcrete
{
    public void WriteNames()
    {
        List<Person> people = new MySimpleDatabase().People;
        foreach (var person in people)
        {
            Console.WriteLine(person.name);
        }
    }
}
public class ProcessPeopleInterface
{
    public void WriteNames()
    {
        //question for here.
        IEnumerable<Person> people = new MySimpleDatabase().People;
        foreach (var person in people)
        {
            Console.WriteLine(person.name);
        }
    }
}

Here I have the concrete processor as well as the interface based one. Here the interface processor is more maintainable of course, but as far as I know I can specify only one type of interface that I require: it is the IEnumerable<Person>.

What if I need something there that implements not one, but two interfaces at the same time. How would I implement that?

So for example, I need something that's not only an IEnumerable<Person>, but also an ICollection? How would I implement that?

Aucun commentaire:

Enregistrer un commentaire