mardi 2 août 2022

How can I get a specific object of specific type from list of objects

I have an animal manager class that has a list of object that implement the IAnimal interface. It has a method that must get the cats only from that same list. How can i design my code in a better way to achieve this ?

Sample code below (in C#)

public interface IAnimal
{
    void doSomeThing();
}

public class Cat : IAnimal
{
    public void doSomeThing()
    {
        Console.WriteLine("Cat");
    }
}

public class Dog : IAnimal
{
    public void doSomeThing()
    {
        Console.WriteLine("Dog");
    }
}

public class AnimalManager
{
    private List<IAnimal> animals = new List<IAnimal>();

    private void manageCat()
    {
     //get cats from animals list.
    }
}

Aucun commentaire:

Enregistrer un commentaire