estoy tratando de entender por qué este reparto no funciona:¿Por qué este ejemplo de fundición de C# no funciona?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CastTest {
class Foo {}
// I want to use this kind of like a typedef, to avoid writing List<Foo> everywhere.
class FooList : List<Foo> {}
class Program {
static void Main(string[] args) {
FooList list = (FooList) Program.GetFooList();
}
// Suppose this is some library method, and i don't have control over the return type
static List<Foo> GetFooList() {
return new List<Foo>();
}
}
}
Esto genera un error de ejecución:
InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[CastTest.Foo]' to type 'CastTest.FooList'.
Puede alguien explicar por qué esto no funciona, y si puedo ¿superar esto de alguna manera?
¿Qué línea genera el error? – ChrisF
¿Qué es correcto decir: un Foolist es una lista, o una lista es un FooList? –
@AnthonyPegram El primero. – hvd