0

I am having issues with filling an object with values. One of the properties is List<IFoo> foos

later when I want to fill my model with data from my database I want to put List<Foo> in place on that property.

I thought since Foo correctly implements the IFoo interface this should work (right?) but I get an error saying that I can't implicity convert List<Foo> to List<IFoo>

I think I am looking past something... Thank you for helping out in advance

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
Pim Schwippert
  • 453
  • 7
  • 18
  • Got a small code sample? – Glen Thomas Jul 24 '18 at 14:13
  • You cannot convert List to List .. but you can cast it: `foos.Cast().ToList()` – richej Jul 24 '18 at 14:14
  • You sure you don't want to convert? `List fooBar = myFoos.ToList();` – JonH Jul 24 '18 at 14:15
  • Yeah sure, giveme a moment and I''ll edit the post – Pim Schwippert Jul 24 '18 at 14:15
  • @PimSchwippert - No need to edit, you can convert your `Foos` to `IFoos` using `ToList()` – JonH Jul 24 '18 at 14:16
  • @JonH I got a json field in my settings table. that settings field can be all sorts of different kinds of settings which is why I made an interface ISettings that doesn't do anything but is just there as a way for me to use implementations like my CustomField which implements the ISetting interface. Does that clear anything up? – Pim Schwippert Jul 24 '18 at 14:17
  • I see, I think that's indeed what I am looking for. Thank you! @JonH – Pim Schwippert Jul 24 '18 at 14:22
  • 1
    @PimSchwippert - No one explains it more clearer than JonSkeet: https://stackoverflow.com/questions/8925400/cast-listt-to-listinterface – JonH Jul 24 '18 at 14:23

2 Answers2

2

Simply put, a List<Sheep> is not a List<IAnimal>even if each Sheep is an IAnimal. Because a List<IAnimal> would allow me to .Add(new BigBadWolf()) which the List<Sheep> can never allow.

What you can do is create a new List<IAnimal> with all your Sheep in it by calling sheep.ToList<IAnimal>().

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • That's a really nice, easy to understand example. – Erndob Jul 24 '18 at 14:34
  • That's very understandable. One question though. if Sheep has a property `bool IsShaved` and IAnimal does not, but I know that my `List` currently only contains `Sheep`, can I cast `List` back to Sheep to access the propert `IsShaved`? – Pim Schwippert Jul 25 '18 at 06:58
  • 1
    @PimSchwippert If your animals are definitely all sheep, you can use `animalList.Cast()` to get an `IEnumerable`. If you are not sure if there are sheep but you want to do something with all sheep, you can use `animalList.OfType()` to also get an `IEnumerable`. Don't forget to add `using System.Linq;` on top of your file if you do that. – nvoigt Jul 25 '18 at 07:39
  • 1
    Alright, thank you so much! :) – Pim Schwippert Jul 25 '18 at 08:02
0

Given types IFoo and Foo:

interface IFoo
{
}

class Foo : IFoo
{
}

You can create a List<IFoo>, populate with instances of Foo and assign to the property/field/variable:

private List<IFoo> foos;

void GetFoos()
{
    foos = new List<IFoo> { new Foo() };
}

But you can't assign List<Foo> to List<IFoo>:

foos = new List<Foo> { new Foo() };
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65