0

I have a base interface

public interface IBase
{
    ...
}

and a interface that derives from this base

public interface IChild : IBase
{
    ...
}

Within my code, I call a method which will return me a List<IBase> (legacy code). With this List I am trying to fill a ObservableCollection<IChild>:

List<IBase> baseList= GetListofBase();
ChildList = new ObservableCollection<IChild>();

// how to fill ChildList with the contents of baseList here?

I know it is not possible to cast from a base to a derived interface, but is it possible to create a derived instance from a base interface?

Roland Deschain
  • 2,211
  • 19
  • 50
  • _"adding the necessary property values to meet the requirements set by the IChild Interface"_ - what? If someone added an instance of a class implementing `IBase` to your list, you can't "add" a property declared in `IChild` to that instance. A `Vertebrate` isn't a `Cat`. Read [ask] and provide a [mre]. You may simply need `listOfBase.OfType()`, but it's really unclear what you're asking. Don't tell about your code, show your code. – CodeCaster Sep 27 '19 at 11:00
  • @CodeCaster what I meant was that IChild implements additional properties to the IBase interface, which I want to add and fill. However, I see how this is written in a rather confusing way - I'll try to update the question. – Roland Deschain Sep 27 '19 at 11:03
  • Explain what you mean by "add and fill". A class implements `IBase`, or `IChild` (and thereby implicitly `IBase`). – CodeCaster Sep 27 '19 at 11:04

2 Answers2

0

Easies aproach for this would be having a constructor in your child class that takes in a IBase.

public interface IBase
{
}

public interface IChild : IBase
{
}

public class ChildClass : IChild
{
    public ChildClass(IBase baseClass) {
        // Do what needs to be done
    }
}

I hope I have understood your question right, as it is a little hard to get what exactly you are looking for.

Milos Romanic
  • 457
  • 3
  • 12
0

You can't fill an ObservableCollection<IChild> with List<IBase>.

You can only fill an ObservableCollection<IBase> with List<IChild> because of inheritance theory rules.

Since IBase is a reduced version of IChild, types can't match: you can't convert IBase to IChild.

Since IChild is an extended version of IBase, types can match: you can convert IChild to IBase.

For example a Toyota car is a Car but all cars are not a Toyota, so you can act on a Toyota as if it is a Car, but you can't act on a Car as if it is a Toyota because a Toyota car have some things and possibilities that abstract Car have not.

Check this tutorial about that, this concept is the same for classes as interfaces:

What is inheritance

The wikipedia page about inheritance:

https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

  • I get the concept of inheritance, however (sticking to the example with cars) if I have a list of IBase (cars), can I add additional information to the items (car is a Toyota) and then add them to a list of IChild? – Roland Deschain Sep 27 '19 at 11:18
  • You can add additional information to type definition and it is what you done when defining `IChild : IBase`, but not to instances at runtime that are of the type predefined by your code... C# don't allow that like in Javascript or Ruby. You can't cast IBase to IChild because doing that is a violation of the OOP theory. –  Sep 27 '19 at 11:20
  • [What is a dynamic language, and why doesn't C# qualify?](https://stackoverflow.com/questions/787239/what-is-a-dynamic-language-and-why-doesnt-c-sharp-qualify) –  Sep 27 '19 at 11:28