クラス P の型パラメーター T に「A から継承する必要がある」という一般的な制約がある場合、最初の呼び出しは成功するが、2 番目の呼び出しはコメントに詳述されている型変換エラーで失敗するのはなぜですか。
abstract class A { }
static class S
{
public static void DoFirst(A argument) { }
public static void DoSecond(ICollection<A> argument) { }
}
static class P<T>
where T : A, new()
{
static void Do()
{
S.DoFirst(new T()); // this call is OK
S.DoSecond(new List<T>()); // this call won't compile with:
/* cannot convert from 'System.Collections.Generic.List<T>'
to 'System.Collections.Generic.ICollection<A>' */
}
}
List<T>
一般的な制約は、それが実際にあることを保証するべきではありませんICollection<A>
か?