Perché non posso fare questo:Perché "come T" riceve un errore ma il casting con (T) non riceve un errore?
public T GetMainContentItem<T>(string moduleKey, string itemKey)
{
return (T)GetMainContentItem(moduleKey, itemKey);
}
ma non questa:
public T GetMainContentItem<T>(string moduleKey, string itemKey)
{
return GetMainContentItem(moduleKey, itemKey) as T;
}
Si lamenta che non ho ristretto il tipo generico abbastanza, ma poi mi potrebbe pensare che regola si applicherebbe alla fusione con "(T)" pure.
La tua risposta è errata, puoi trasmettere i tipi di valore, il risultato sarà annullabile, vedi questo post e la risposta di Jon Skeet: http://stackoverflow.com/questions/496096/casting-vs-using-the -come-parola chiave-in-the-clr – Mikhail