Tengo dos clases: una clase base (Animal) y una clase derivada de it (Cat). La clase Base contiene un método virtual Play que toma List como parameter.Something de entrada como estaCasting List <> de la clase derivada a List <> de la clase base
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication9
{
class Animal
{
public virtual void Play(List<Animal> animal) { }
}
class Cat : Animal
{
public override void Play(List<Animal> animal)
{
}
}
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat();
cat.Play(new List<Cat>());
}
}
}
cuando compilo el programa anterior, me sale el siguiente error
Error 2 Argument 1: cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List'
¿hay alguna forma de lograr esto?
posible duplicado de [Casting una colección genérica de tipo base] (http: //stackoverflow.com/questions/539287/casting-a-generic-collection-to-base-type) – finnw
Lo primero que hay que hacer es cambiar el argumento List <> a un argumento IEnumerable <>. –
posible duplicado de [Convertir lista <> de objetos de clase derivados a List <> de objetos de clase base] (http://stackoverflow.com/questions/1817300/convert-list-of-derived-class-objects-to-list -of-base-class-objects) –