Using legacy interfaces in interface implementations in C #

How to implement functions that receive inherited interfaces? I have these interfaces:

interface IAnimal
interface IDog : IAnimal
interface ICat : IAnimal

interface IShelter
class DogShelter : IShelter
class CatShelter : IShelter

Now I want IShelter to have a function:

Store(IAnimal animal)

but I want DogShelter to implement it like this:

Store(IDog animal) 

and CatShelter:

Store(ICat animal).

Is there any way to do this? Other than using DogShelter Store (IAnmial animal) and checking with "if (the animal is an IDog)"?

Should I go with the Store (IAnimal animal) and then drop it with the (IDog) animal?

(I would like to use interface inheritance with respect to IDog and ICat. Class inheritance is not possible in real code) (Computing time is very important right now. Is it cheaper to use Store (IDog animal) instead of checking if (animal is an IDog)? Or is it just convenience?)

+4
1

. .

        interface IShelter<T> where T : IAnimal
    {
        void Store(T animal);
    }
    class DogShelter : IShelter<IDog>
    {
        public void Store(IDog animal)
        {
            throw new NotImplementedException();
        }
    }
    class CatShelter : IShelter<ICat>
    {
        public void Store(ICat animal)
        {
            throw new NotImplementedException();
        }
    }
+5

Source: https://habr.com/ru/post/1618342/


All Articles