PassionateDeveloper
PassionateDeveloper

Reputation: 15138

Best Practice of Inheritance: Property should be an interface too or not?

I have 2 interfaces and 2 classes:

public interface interface A
{
     List<B> MyList { get; set; }
}

public interface interface B
{
    string Name { get; set; }
}

public class ImplementA : A
{
     List<ImplementB> MyList { get; set; }
}

public class ImplementB : B
{
    string Name { get; set; }
}

So this gives me an error because ImplementA doesn't fullfill the Interface A. It doesn't because I would have to have the property List of interface B instead of List of ImplementB.

What is the best practice to fullfill the Interface but still be able to have a concrete classe within classes AND a abstract interface in the interface?

Upvotes: 0

Views: 55

Answers (1)

aepot
aepot

Reputation: 4824

The example is very friendly to make it Generic.

public interface A<T>
{
     List<T> MyList { get; set; }
}

public interface B
{
    string Name { get; set; }
}

public class ImplementA : A<ImplementB>
{
    public List<ImplementB> MyList { get; set; }
}

public class ImplementB : B
{
    public string Name { get; set; }
}

Or this one, upon your needs

public class ImplementA : A<B>
{
    public List<B> MyList { get; set; }
}

Upvotes: 1

Related Questions