Dlaczego mogę to zrobić:Dlaczego "as T" otrzymuje błąd, ale rzutowanie z (T) nie powoduje błędu?
public T GetMainContentItem<T>(string moduleKey, string itemKey)
{
return (T)GetMainContentItem(moduleKey, itemKey);
}
ale nie w ten sposób:
public T GetMainContentItem<T>(string moduleKey, string itemKey)
{
return GetMainContentItem(moduleKey, itemKey) as T;
}
Zarzuca, że nie ogranicza typ rodzajowy mało, ale myślę, że zasada ta stosuje się do odlewania również z "(T)".
Twoja odpowiedź jest niepoprawna, możesz rzutować na typy wartości, wynik będzie zerowy, zobacz ten post i odpowiedź Jona Skeeta: http://stackoverflow.com/questions/496096/casting-vs-using-the -as-keyword-in-the-clr – Mikhail