e.g.:
class Program
{
static void Main(string[] args){
List<Banana> bananas = new List<Banana>(); List<IFruit> fruitsFail = bananas; //Error 1 Cannot implicitly convert type 'System.Collections.Generic.Listforeach (Banana b in bananas)
fruitsWork.Add(b);
}
}
interface IFruit { } class Apple : IFruit { } class Banana : IFruit { }As you see i am able to iterate through the collection and cast every element to build a new list. Can somebody tell a more elegant way to get a list typed to the interface or point to my error in reasoning? Thanks a lot!
Replies
Know the answer? Post it — somebody with the same question will find it here.