私がインターフェースとクラスを持っていると仮定します:
public interface ITree {}
public class Tree : ITree {}
共変IEnumerable<T>
であるように、以下のコード行は正常にコンパイルされます。
IEnumerable<ITree> trees = new List<Tree>();
しかし、私がそれをジェネリックメソッドに入れると:
public void Do<T>() where T : ITree
{
IEnumerable<ITree> trees = new List<T>();
}
コンパイラからコンパイル済みエラーが発生します:
エラー1タイプ'System.Collections.Generic.List'を'System.Collections.Generic.IEnumerable'に暗黙的に変換できません。明示的な変換が存在します(キャストがありませんか?)D:\ lab \ Lab.General \ Lab.General \ Program.cs 83 40 Lab.General
この場合、なぜ共分散が機能しないのですか?