Why can I not assign a List of concrete types to a List of that concrete's interface?

前端 未结 8 1496
悲哀的现实
悲哀的现实 2020-12-19 02:17

Why does this not compile?

public interface IConcrete { }

public class Concrete : IConcrete { }

public class Runner
{
    public static void Main()
    {
         


        
8条回答
  •  执笔经年
    2020-12-19 02:56

    This has to do with covariance and contravariance. Eric Lippert wrote a lot about it earlier this year. (11 blog entries specifically on that topic.) The first one is Covariance and Contravariance in C#, Part One. Read that and search his blog for the rest of them. He provides detailed explanations of why this kind of thing is difficult.

    Good news: some of the restrictions are lifted in C# 4.0.

提交回复
热议问题