che sto cercando di capire perché questo cast non funziona:Perché questo esempio di casting C# non funziona?
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>();
}
}
}
Questo genera un errore di runtime:
InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[CastTest.Foo]' to type 'CastTest.FooList'.
Qualcuno può spiegare perché questo non funziona, e se posso andare in giro in qualche modo?
Quale riga genera l'errore? – ChrisF
Quale è corretto dire: A FooList è una lista oppure una lista è FooList? –
@AnthonyPegram Il primo. – hvd