2
Answers

Question about Inheritence in C#

Nader

Nader

16y
2.5k
1

Hello,

First, here's a sample code of what I'm trying to do:


using
System.Collections.Generic;

namespace MyTest
{

public interface
IZoo
{
   List<Animal> Animals { get; set; }
}

public class MyHome : IZoo
{
   List<Animal> MyDogs = new List<Dog>();
}

class Animal
{}

class Dog : Animal
{}

}


The code above does not work in C#. It genereates the following error:
Error 1 'MyTest.MyHome' does not implement interface member 'MyTest.IZoo.Animals'

Basically I'm trying to implement an interface that has a property like this:
   List<Animal> Animals { get; set; }

But I don't want to implement it exactly as it is, I want to implement it as a more specific list like this:
   List<Animal> MyDogs = new List<Dog>();

or like this:
   List<Dog> MyDogs = new List<Dog>();

There must be a way to do that... what am I missing?

Thanks,

Nader

Answers (2)