Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert from List<Bar> to List<Foo>

I have a set up like this:

abstract class Foo {} class Bar : Foo {} 

and a method elsewhere of this form:

void AddEntries(List<Foo>) {} 

I am trying to call this method using a list of objects of type Bar

List<Bar> barList = new List<Bar>() AddEntries(barList); 

but this gives me the error:

cannot convert from List<Bar> to List<Foo>

Is there anyway around this problem? I Need to keep the method definition using the abstract class.

like image 356
jonnybolton Avatar asked Jul 27 '17 11:07

jonnybolton


1 Answers

You could make your AddEntries generic and change it to this

void AddEntries<T>(List<T> test) where T : Foo {     //your logic  } 

Have a look at Constraints on Type Parameters for further information.

like image 101
Mighty Badaboom Avatar answered Sep 18 '22 05:09

Mighty Badaboom